-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerMovement.cs
44 lines (33 loc) · 1.13 KB
/
PlayerMovement.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
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 = 12f;
public float gravity = -9.8f;
public float jumpheight = 6f;
public Transform groundcheck;
public float groundistance = 0.4f;
public LayerMask groundmask;
Vector3 velocity;
bool isgrounded;
// Update is called once per frame
void Update()
{
isgrounded = Physics.CheckSphere(groundcheck.position, groundistance, groundmask);
if (isgrounded && velocity.y < 0){
velocity.y = -4f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
Controller.Move(move * speed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isgrounded){
velocity.y = jumpheight;
}
velocity.y += gravity * Time.deltaTime * 2;
Controller.Move(velocity * Time.deltaTime);
}
}