89 lines
1.8 KiB
C#
89 lines
1.8 KiB
C#
using UnityEngine;
|
|
|
|
public class FirstPersonFlyingController : MonoBehaviour
|
|
{
|
|
public float speed = 1f;
|
|
|
|
public Transform cameraTransform;
|
|
|
|
private Transform t;
|
|
|
|
private Vector3 movementVectorSmooth;
|
|
|
|
private Vector3 rotationVectorSmooth;
|
|
|
|
public GameObject checkSphere;
|
|
|
|
public bool dontRequireClick;
|
|
|
|
public bool limitPosition;
|
|
|
|
public Vector3 minPosition;
|
|
|
|
public Vector3 maxPosition;
|
|
|
|
private void Start()
|
|
{
|
|
t = GetComponent<Transform>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
Movement();
|
|
MouseLook();
|
|
}
|
|
|
|
private void Movement()
|
|
{
|
|
Vector3 zero = Vector3.zero;
|
|
if (Input.GetKey(KeyCode.W))
|
|
{
|
|
zero.z += 1f;
|
|
}
|
|
if (Input.GetKey(KeyCode.S))
|
|
{
|
|
zero.z -= 1f;
|
|
}
|
|
if (Input.GetKey(KeyCode.A))
|
|
{
|
|
zero.x -= 1f;
|
|
}
|
|
if (Input.GetKey(KeyCode.D))
|
|
{
|
|
zero.x += 1f;
|
|
}
|
|
if (Input.GetKey(KeyCode.Space))
|
|
{
|
|
zero.y += 1f;
|
|
}
|
|
if (Input.GetKey(KeyCode.LeftShift))
|
|
{
|
|
zero.y -= 1f;
|
|
}
|
|
zero = Vector3.Normalize(zero);
|
|
movementVectorSmooth = Vector3.Lerp(movementVectorSmooth, zero, 5f * Time.deltaTime);
|
|
t.Translate(movementVectorSmooth * 9f * Time.deltaTime * speed);
|
|
if (limitPosition)
|
|
{
|
|
Vector3 position = t.position;
|
|
position.x = Mathf.Clamp(position.x, minPosition.x, maxPosition.x);
|
|
position.y = Mathf.Clamp(position.y, minPosition.y, maxPosition.y);
|
|
position.z = Mathf.Clamp(position.z, minPosition.z, maxPosition.z);
|
|
t.position = position;
|
|
}
|
|
}
|
|
|
|
private void MouseLook()
|
|
{
|
|
Vector3 zero = Vector3.zero;
|
|
if (Input.GetKey(KeyCode.Mouse0) || dontRequireClick)
|
|
{
|
|
zero.y += Input.GetAxis("Mouse X") * 1f;
|
|
zero.x -= Input.GetAxis("Mouse Y") * 1f;
|
|
}
|
|
rotationVectorSmooth = Vector3.Lerp(rotationVectorSmooth, zero, 5f * Time.deltaTime);
|
|
t.Rotate(new Vector3(0f, rotationVectorSmooth.y * 2f, 0f));
|
|
cameraTransform.Rotate(Vector3.right * rotationVectorSmooth.x * 2f);
|
|
}
|
|
}
|