Advertisement
Guest User

PlayerMovement

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