-
Notifications
You must be signed in to change notification settings - Fork 2
/
Movement_CC_TopDown.cs
63 lines (55 loc) · 1.92 KB
/
Movement_CC_TopDown.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class Movement_CC_TopDown : MonoBehaviour
{
//Movement
[Header("Settings Camera")]
[SerializeField] private Camera _Camera;
[Header("Settings")]
[SerializeField] private float _NormalSpeed = 5;
[SerializeField] private float _SprintSpeed = 8;
[SerializeField] private float _JumpSpeed = 5;
[SerializeField] private float _Gravity = 20;
[SerializeField] private bool _MovementRelativeToRotation = false;
private float _Speed = 0;
private Vector3 _MoveDirection = Vector3.zero;
private CharacterController _CC;
void Start()
{
_CC = GetComponent<CharacterController>();
}
void Update()
{
//Movement
if (_CC.isGrounded)
{
_MoveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (_MovementRelativeToRotation)
_MoveDirection = transform.TransformDirection(_MoveDirection);
_MoveDirection *= _Speed;
if (Input.GetButton("Jump"))
_MoveDirection.y = _JumpSpeed;
}
_MoveDirection.y -= _Gravity * Time.deltaTime;
_CC.Move(_MoveDirection * Time.deltaTime);
//Sprint
if (Input.GetKey(KeyCode.LeftShift))
_Speed = _SprintSpeed;
else
_Speed = _NormalSpeed;
Ray cameraRay = _Camera.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, Vector3.zero);
float rayLength;
if (groundPlane.Raycast(cameraRay, out rayLength))
{
Vector3 pointToLook = cameraRay.GetPoint(rayLength);
transform.LookAt(new Vector3(pointToLook.x, transform.position.y, pointToLook.z));
}
}
public void SetCamera(Camera cameraobj)
{
_Camera = cameraobj;
}
}