Advertisement
jretchy

Untitled

May 15th, 2021
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1. [Header("Player Movement")]
  2.     public CharacterController controller;
  3.     public float speed = 12f;
  4.  
  5.     [Header("Jumping")]
  6.     public float gravity = -9.81f;
  7.     public float jumpHeight = 3f;
  8.     public Transform groundCheck;
  9.     public float groundDistance = 0.4f;
  10.     public LayerMask groundMask;
  11.  
  12.     Vector3 velocity;
  13.     bool isGrounded;
  14.  
  15.     private Vector3 moveVector;
  16.  
  17.     private void Update()
  18.     {
  19.  
  20.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
  21.  
  22.         if (isGrounded && velocity.y < 0)
  23.         {
  24.             velocity.y = -2f;
  25.         }
  26.  
  27.         float x = Input.GetAxis("Horizontal");
  28.         float z = Input.GetAxis("Vertical");
  29.  
  30.         Vector3 move = transform.right * x + transform.forward * z;
  31.  
  32.         controller.Move(move * speed * Time.deltaTime);
  33.  
  34.         if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
  35.         {
  36.             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
  37.             FindObjectOfType<SoundController>().jumpingSound.Play();
  38.         }
  39.  
  40.         velocity.y += gravity * Time.deltaTime;
  41.  
  42.         controller.Move(velocity * Time.deltaTime);
  43.  
  44.  
  45.  
  46.     }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement