Advertisement
Guest User

playerMovement.cs

a guest
Jan 17th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7.     public CharacterController controller;
  8.  
  9.     public float speed;
  10.     public float gravity = -19.81f; //default gravity
  11.     public float jumpHeight = 3f;
  12.  
  13.     public Transform groundCheck; //invisible object on player's feet
  14.     public float groundDistance = 0.2f; //radius of the invisible sphere
  15.     public LayerMask groundMask; //controlling which objects the sphere should check for, so that it doesnt check isGrounded as true when standing on the player
  16.  
  17.     Vector3 velocity;
  18.     bool isGrounded;
  19.  
  20.     // Update is called once per frame
  21.     void Update()
  22.     {
  23.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
  24.         //creates a tiny invisible sphere on player's foot with specified radius groundDistance, and if it collides with anything in groundMask, then isGrounded will set to true, if not, then false
  25.  
  26.         if (isGrounded && velocity.y < 0)
  27.         {
  28.             velocity.y = -2f; //not 0 but -2 is because if incase it sets true too soon, it makes the player force on ground properly
  29.         }
  30.  
  31.         float x = Input.GetAxis("Horizontal"); //getaxis function gives 1 if right key is pressed
  32.         float z = Input.GetAxis("Vertical"); //1 if forwards key is pressed
  33.  
  34.         /* first dumb idea - https://pastebin.com/CFXXtY8s*/
  35.  
  36.         Vector3 move = (transform.right * x + transform.forward * z) * speed; //transform right is red arrow vector, from -1 to 1, forward is blue axis
  37.         //those arrows are multiplied by x or z, which are 0 if nothing is pressed or 1 if wasd is pressed
  38.         //then its added together, forming a vector pointing to the direction where i wanna go
  39.         //and multiplied by the speed constant
  40.        
  41.         controller.Move(move * Time.deltaTime);
  42.  
  43.  
  44.         /*first dumb idea  - gravity multiplier makes it grow more each time, but its would be better to square the velocity by gravity multiplier so it grows exponencialy
  45.         velocity = velocity * gravityMultiplier;
  46.         gravityMultiplier++;*/
  47.  
  48.         if (Input.GetButtonDown("Jump") && isGrounded) //jumping
  49.         {
  50.             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity); //why? physics https://imgur.com/a/BQMlYuj
  51.         }
  52.  
  53.         velocity.y = velocity.y + gravity * Time.deltaTime;
  54.         controller.Move(velocity * Time.deltaTime);
  55.         //2x delta time - ^2, and why its like this - physics https://imgur.com/a/ulUTu7D
  56.  
  57.         //Debug.Log("transform.right " + transform.right);
  58.         //Debug.Log("x " + x);
  59.         //Debug.Log("transform.forward" + transform.forward);
  60.         //Debug.Log("z " + z);
  61.         //Debug.Log("move " + move);
  62.  
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement