60 lines
1.8 KiB
C#
60 lines
1.8 KiB
C#
using UnityEngine;
|
|
|
|
public class CameraMove : MonoBehaviour
|
|
{
|
|
[SerializeField] private CharacterController _characterController;
|
|
[SerializeField] private Transform _playerRoot;
|
|
[SerializeField] private Transform _camera;
|
|
|
|
[Space(10)]
|
|
[SerializeField] private float _moveSpeed = 2;
|
|
[SerializeField] private float _rotateSpeed = 2;
|
|
|
|
[Space(10)]
|
|
[SerializeField] private float _minWorldY;
|
|
|
|
|
|
private float _yaw = 0;
|
|
private float _tilt = 0;
|
|
|
|
private void Awake()
|
|
{
|
|
_yaw = _playerRoot.eulerAngles.y;
|
|
_tilt = _camera.localEulerAngles.x;
|
|
}
|
|
|
|
|
|
|
|
private void Update()
|
|
{
|
|
// Rotate
|
|
if (Input.GetMouseButton(1))
|
|
{
|
|
_yaw += Input.GetAxis("Mouse X") * _rotateSpeed;
|
|
_tilt -= Input.GetAxis("Mouse Y") * _rotateSpeed;
|
|
|
|
_tilt = Mathf.Clamp(_tilt, -89, 89);
|
|
|
|
_playerRoot.eulerAngles = new Vector3(0, _yaw, 0);
|
|
_camera.localEulerAngles = new Vector3(_tilt, 0, 0);
|
|
}
|
|
|
|
// Move
|
|
Vector3 dir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
|
|
float height = Mathf.Max(0, _camera.localPosition.y + ((Input.GetKey(KeyCode.Q) ? -_moveSpeed : 0) + (Input.GetKey(KeyCode.E) ? _moveSpeed : 0)) * Time.deltaTime);
|
|
|
|
dir = Quaternion.Euler(_camera.localEulerAngles.x, _playerRoot.localEulerAngles.y, _camera.localEulerAngles.z) * dir;
|
|
_characterController.Move(dir * _moveSpeed * Time.deltaTime);
|
|
|
|
if (_playerRoot.position.y < _minWorldY)
|
|
{
|
|
Vector3 position = _playerRoot.position;
|
|
position.y = _minWorldY;
|
|
_playerRoot.position = position;
|
|
}
|
|
|
|
_playerRoot.position = new Vector3(_playerRoot.position.x, _camera.position.y, _playerRoot.position.z);
|
|
_camera.localPosition = Vector3.zero;
|
|
}
|
|
}
|