77 lines
1.8 KiB
C#
77 lines
1.8 KiB
C#
using UnityEngine;
|
|||
|
|
using System.Collections;
|
||
|
|
|
||
|
|
|
||
|
|
[ExecuteInEditMode]
|
||
|
|
public class CameraController : MonoBehaviour {
|
||
|
|
public bool Active = false;
|
||
|
|
public float sensitivity = 2F;
|
||
|
|
|
||
|
|
float rotationY = 0F;
|
||
|
|
|
||
|
|
Vector3 Speed = new Vector3();
|
||
|
|
public float MaxSpeed = 10f;
|
||
|
|
public float AccelerationTime = 1f;
|
||
|
|
public float BrakingTime = 0.5f;
|
||
|
|
|
||
|
|
|
||
|
|
void Start ()
|
||
|
|
{
|
||
|
|
rotationY = -transform.localEulerAngles.x;
|
||
|
|
}
|
||
|
|
|
||
|
|
void Update ()
|
||
|
|
{
|
||
|
|
if(Input.GetKeyDown("mouse 1")){
|
||
|
|
Active = !Active;
|
||
|
|
}
|
||
|
|
|
||
|
|
Screen.lockCursor = Active;
|
||
|
|
|
||
|
|
if (!Active) return;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivity;
|
||
|
|
|
||
|
|
rotationY += Input.GetAxis("Mouse Y") * sensitivity;
|
||
|
|
rotationY = Mathf.Clamp (rotationY, -90, 90);
|
||
|
|
|
||
|
|
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
|
||
|
|
|
||
|
|
|
||
|
|
Vector3 AccelerationForce = new Vector3(
|
||
|
|
((Input.GetKey("d")? 1f:0f)-(Input.GetKey("a")? 1f:0f)),
|
||
|
|
((Input.GetKey("space")? 1f:0f)-(Input.GetKey("left ctrl")? 1f:0f)),
|
||
|
|
((Input.GetKey("w")? 1f:0f)-(Input.GetKey("s")? 1f:0f))
|
||
|
|
);
|
||
|
|
AccelerationForce *= (Time.deltaTime/AccelerationTime);
|
||
|
|
Speed += AccelerationForce;
|
||
|
|
|
||
|
|
Vector3 BrakingForce = new Vector3(
|
||
|
|
(Input.GetKey("d")|Input.GetKey("a"))? 0f:1f,
|
||
|
|
(Input.GetKey("space")|Input.GetKey("left ctrl"))? 0f:1f,
|
||
|
|
(Input.GetKey("w")|Input.GetKey("s"))? 0f:1f
|
||
|
|
);
|
||
|
|
|
||
|
|
BrakingForce *= (Time.deltaTime/BrakingTime);
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
Speed.Set(
|
||
|
|
Speed.x-(Mathf.Sign(Speed.x)*Mathf.Min(BrakingForce.x,Mathf.Abs(Speed.x))),
|
||
|
|
Speed.y-(Mathf.Sign(Speed.y)*Mathf.Min(BrakingForce.y,Mathf.Abs(Speed.y))),
|
||
|
|
Speed.z-(Mathf.Sign(Speed.z)*Mathf.Min(BrakingForce.z,Mathf.Abs(Speed.z))));
|
||
|
|
|
||
|
|
Speed.Set(
|
||
|
|
Mathf.Clamp(Speed.x,-1f,1f),
|
||
|
|
Mathf.Clamp(Speed.y,-1f,1f),
|
||
|
|
Mathf.Clamp(Speed.z,-1f,1f));
|
||
|
|
|
||
|
|
|
||
|
|
transform.Translate(Time.deltaTime*Speed*MaxSpeed,Space.Self);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|