Advertisement
leomovskii

PlayerMovement.cs

Nov 8th, 2021
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour {
  6.  
  7.     CharacterController controller;
  8.  
  9.     public float moveSpeed = 12f;
  10.     public float gravity = 9.81f;
  11.     public float jumpHeight = 3f;
  12.  
  13.     Vector3 velocity;
  14.     bool isGrounded;
  15.  
  16.     public Transform groundCheck;
  17.     public float groundDistance = 0.4f;
  18.     public LayerMask groundMask;
  19.  
  20.     void Start() {
  21.         controller = GetComponent<CharacterController>();
  22.     }
  23.  
  24.     void Update() {
  25.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
  26.  
  27.         if (isGrounded && velocity.y < 0)
  28.             velocity.y = -2f;
  29.  
  30.         float x = Input.GetAxis("Horizontal");
  31.         float z = Input.GetAxis("Vertical");
  32.  
  33.         Vector3 move = transform.right * x + transform.forward * z;
  34.  
  35.         controller.Move(move * moveSpeed * Time.deltaTime);
  36.  
  37.         if (Input.GetButtonDown("Jump") && isGrounded) {
  38.             velocity.y = Mathf.Sqrt(jumpHeight * 2f * gravity);
  39.         }
  40.  
  41.         velocity.y -= gravity * Time.deltaTime;
  42.         controller.Move(velocity * Time.deltaTime);
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement