-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerMovement.cs
More file actions
55 lines (44 loc) · 1.43 KB
/
PlayerMovement.cs
File metadata and controls
55 lines (44 loc) · 1.43 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody rb;
public float moveSpeed;
public float jumpForce;
public float sprintMultiplier;
public bool isGrounded;
public LayerMask Ground;
public GameObject GroundCheck;
public Transform orientation;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
transform.rotation = orientation.rotation;
//input
float x = Input.GetAxis("Horizontal") * moveSpeed;
float y = Input.GetAxis("Vertical") * moveSpeed;
//moving
Vector3 movePos = orientation.right * x + orientation.forward * y;
Vector3 newMovePos = new Vector3(movePos.x, rb.velocity.y, movePos.z);
if (isGrounded)
{
rb.velocity = newMovePos;
}
//Grounded
isGrounded = Physics.CheckSphere(GroundCheck.transform.position, 0.2f, Ground);
//sprinting
if (Input.GetKeyDown(KeyCode.LeftShift))
moveSpeed *= sprintMultiplier;
else if (Input.GetKeyUp(KeyCode.LeftShift))
moveSpeed /= sprintMultiplier;
//Jumping
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z);
}
}
}