-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameraMovementTMP.cs
82 lines (71 loc) · 2.34 KB
/
CameraMovementTMP.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using UnityEngine;
using System.Collections;
//wasd to move forward, backward, sideways, space to move up, shift to move down
//mouse to look around
public class CameraMovementTMP : MonoBehaviour {
public GameObject water;
private float forceMultiplier = 40;
private float dragForce = 2;
private bool jump = false;
private bool water_speed = false;
void Start () {
gameObject.GetComponent<Rigidbody>().drag = dragForce;
}
void Update () {
//compute forward, left, and right (relative to camera's rotation)
Vector3 forward = gameObject.transform.forward;
forward = new Vector3 (forward.x, 0, forward.z);
forward = forward.normalized;
Vector3 left = Vector3.Cross (forward, Vector3.up);
Vector3 right = Vector3.Cross (forward, Vector3.down);
//forward backward
if (Input.GetKey ("w")) {
gameObject.GetComponent<Rigidbody>().AddForce (forceMultiplier * forward);
} else if (Input.GetKey ("s")) {
gameObject.GetComponent<Rigidbody>().AddForce (-1 * forceMultiplier * forward);
}
//left right
if (Input.GetKey ("a")) {
gameObject.GetComponent<Rigidbody>().AddForce (forceMultiplier * left );
} else if (Input.GetKey ("d")) {
gameObject.GetComponent<Rigidbody>().AddForce (forceMultiplier * right);
}
//up down
if (Input.GetKey ("space")) {
gameObject.GetComponent<Rigidbody>().AddForce (forceMultiplier * Vector3.up);
} else if (Input.GetKey (KeyCode.LeftShift)) {
gameObject.GetComponent<Rigidbody>().AddForce (forceMultiplier * Vector3.down);
}
if (Input.GetKey ("e") && jump == true) {
gameObject.GetComponent<Rigidbody>().AddForce (forceMultiplier * Vector3.up*30);
jump = false;
}
//rotate view
Vector2 mp = Input.mousePosition;
float pitch = Mathf.Lerp (-89, 89, 1 - mp.y / Screen.height);
float yaw = Mathf.Lerp (-180, 180, ProperMod (mp.x, Screen.width) / Screen.width);
GetComponent<Rigidbody>().rotation = Quaternion.Euler (new Vector3 (pitch, yaw, 0));
}
void OnCollisionEnter(Collision col){
if (col.gameObject == water) {
if(water_speed == false){
dragForce = 50;
water_speed = true;
}
else {
dragForce = 2;
water_speed = false;
}
}else{
jump = true;
}
}
//returns a % b that works on negative numbers
float ProperMod (float a, float b) {
if (a > 0) return a % b;
else {
int quotient = (int) (a / b);
return a - (quotient - 1) * b;
}
}
}