-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMovement_CC_FirstPerson.cs
More file actions
72 lines (62 loc) · 2.18 KB
/
Copy pathMovement_CC_FirstPerson.cs
File metadata and controls
72 lines (62 loc) · 2.18 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class Movement_CC_FirstPerson : MonoBehaviour
{
[Header("Settings")]
[SerializeField] private float _NormalSpeed = 5;
[SerializeField] private float _SprintSpeed = 8;
[SerializeField] private float _JumpSpeed = 5;
[SerializeField] private float _Gravity = 20;
[SerializeField] private float _CameraSensitivity = 1;
[Header("Head")]
[SerializeField] private Transform _Head = null;
//Private Variables
private Vector3 _MoveDirection;
private Vector2 _LookRotation;
private CharacterController _CC;
private bool _LockRotation;
private float _Speed;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
_CC = GetComponent<CharacterController>();
if (_Head == null)
_Head = transform.GetChild(0).transform;
}
void Update()
{
//Look around
if (!_LockRotation)
{
_LookRotation.x += Input.GetAxis("Mouse X") * _CameraSensitivity;
_LookRotation.y += Input.GetAxis("Mouse Y") * _CameraSensitivity;
_LookRotation.y = Mathf.Clamp(_LookRotation.y, -90, 90);
transform.localRotation = Quaternion.AngleAxis(_LookRotation.x, Vector3.up);
_Head.transform.localRotation = Quaternion.AngleAxis(_LookRotation.y, Vector3.left);
}
//Movement
if (_CC.isGrounded)
{
_MoveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
_MoveDirection = transform.TransformDirection(_MoveDirection);
_MoveDirection *= _Speed;
if (Input.GetButton("Jump"))
_MoveDirection.y = _JumpSpeed;
}
//Sprint
if (Input.GetKey(KeyCode.LeftShift))
_Speed = _SprintSpeed;
else
_Speed = _NormalSpeed;
//Apply Movement
_MoveDirection.y -= _Gravity * Time.deltaTime;
_CC.Move(_MoveDirection * Time.deltaTime);
}
public void LockRotation(bool state)
{
_LockRotation = state;
}
}