-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayermovementsprint.cs
58 lines (40 loc) · 1.38 KB
/
playermovementsprint.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playermovement : MonoBehaviour
{
// Start is called before the first frame update
public CharacterController controller;
public float speed = 15f;
public float gravity = -9.8f;
public float jumpHeight = 2f;
Vector3 downVel;
public Transform groundCheck;
public float groundDistance = 0.5f;
public LayerMask groundMask;
bool isGrounded;
// Update is called once per frame
void Update()
{
Debug.Log(isGrounded);
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && downVel.y < 0 ) {
downVel.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
if(Input.GetKey(KeyCode.LeftShift)){
controller.Move(move * speed * Time.deltaTime * 1.5f);
}
else{
controller.Move(move * speed * Time.deltaTime);
}
if(Input.GetKeyDown(KeyCode.Space) && isGrounded) {
downVel.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
Debug.Log("Jump");
}
downVel.y += gravity * Time.deltaTime;
controller.Move(downVel * Time.deltaTime);
}
}