Repository navigation
Expand file tree
/
Copy pathcharacterKeyboardController.cs
More file actions
69 lines (49 loc) · 1.93 KB
/
Copy pathcharacterKeyboardController.cs
File metadata and controls
69 lines (49 loc) · 1.93 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
using UnityEngine;
//Important Note!!!
//You need a character controller for this code to work
public class PlayerMovement : MonoBehaviour
{
//You need to attach your camera
public Transform cam;
//You need to attach your character controller
public CharacterController controller;
//walk speed of your character
public float speed = 6f;
//How smooth your character turn
public float TurnSmooth = 0.1f;
//velocity of smooth turn
float turnSmoothVelocity;
private float verticalvelocity;
//Gravity
public float gravity = 9.8f;
//Jump Force of your character
public float jumpForce = 6f;
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if(direction.magnitude >= 0.01f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, TurnSmooth);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
if (controller.isGrounded)
{
verticalvelocity = -gravity * Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space))
{
verticalvelocity = jumpForce;
}
}
else
{
verticalvelocity -= gravity * Time.deltaTime;
}
Vector3 moveVector = new Vector3(0, verticalvelocity, 0);
controller.Move(moveVector * Time.deltaTime);
}
}