-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerMove.cs
More file actions
61 lines (50 loc) · 1.54 KB
/
PlayerMove.cs
File metadata and controls
61 lines (50 loc) · 1.54 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
public CharacterController controller;
private float walkSpeed = 15f;
private float sprintSpeed = 23f;
private float moveSpeed;
//gravity variables
public float grav = -9.81f;
Vector3 velocity;
private bool isGrounded;
public float jumpHeight = 3f;
public Transform groundCheck;
public float groundDistance = 0.3f;
public LayerMask groundMask;
// Update is called once per frame
void Update()
{
KeyboardMovement();
}
void KeyboardMovement()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * walkSpeed * Time.deltaTime);
//gravity pull
velocity.y += grav * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f + grav);
}
if (Input.GetKeyDown(KeyCode.LeftShift) && isGrounded)
{
moveSpeed = sprintSpeed;
}
else
{
moveSpeed = walkSpeed;
}
}
}