This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
moveCam.cs
46 lines (40 loc) · 1.72 KB
/
moveCam.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Move the camera of the scene,
// With controls similar of the FPS-ones
// Move forward/backward and strafe left/right with pressing the directional keys
// Rotate the camera and going up/down with the mouse
// A rotation matrix is created
// to always strafe following the perpendicular directions
// to where the camera is pointing at
public class moveCam : MonoBehaviour {
float speed = 0.1f;
float sensitivity = 0.4f;
float moveUD, moveLR, rotX, rotY;
Matrix4x4 Perp; // Rotation matrix
void Start () {
Perp = Matrix4x4.zero;
Perp.m11 = 1;
Perp.m00 = Mathf.Cos(90*Mathf.PI/180);
Perp.m22 = Mathf.Cos(90*Mathf.PI/180);
Perp.m02 = Mathf.Sin (90 * Mathf.PI / 180);
Perp.m20 = -Mathf.Sin (90 * Mathf.PI / 180);
// Initializing the camera to make it pointing at the scene
transform.position = new Vector3 (0, main.tankHeight/2 , -main.tankHeight* 8 / 3);
transform.rotation = Quaternion.Euler (new Vector3(10, 2, 0));
}
// Update is called once per frame
void Update () {
moveUD = Input.GetAxis ("Vertical") * speed; // getting the user control inputs
moveLR = Input.GetAxis ("Horizontal") * speed;
rotX = Input.GetAxis ("Mouse X") * sensitivity;
rotY = Input.GetAxis ("Mouse Y") * sensitivity;
transform.Rotate (0,rotX,0); // rotating the camera
Vector3 ortho = Perp * transform.forward; // getting the perpendicular direction (strafe)
Vector3 movement = new Vector3(moveLR * ortho.x + moveUD*transform.forward.x,
Mathf.Sin (rotY) * sensitivity,
moveLR * ortho.z + moveUD*transform.forward.z);
transform.position += movement; // translating the camera
}
}