Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.12 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class StandardMovement : MonoBehaviour {
  6.  
  7.     //X & Z Movement
  8.     public int moveSpeed;
  9.     public int jumpHeight;
  10.     float moveHor;
  11.     float moveVer;
  12.     Rigidbody localPlayerRB;
  13.  
  14.     //Y Movement
  15.     bool isGrounded;
  16.     bool hasWaitedForJump = true;
  17.     public float playerDistFromGround;
  18.     public float jumpFeatherDelay;
  19.  
  20.     void Start () {
  21.         localPlayerRB = GetComponent<Rigidbody>();
  22.     }
  23.    
  24.     void FixedUpdate () {
  25.         //Checks for input
  26.         moveHor = Input.GetAxis("Horizontal");
  27.         moveVer = Input.GetAxis("Vertical");
  28.  
  29.         //Neutralizes moveY. (We dont want X & Z movement to effect the Y velocity
  30.         float moveY = localPlayerRB.velocity.y;
  31.        
  32.         //Updates the RB velocity according to Player input
  33.         localPlayerRB.velocity = new Vector3(moveHor * moveSpeed, moveY, moveVer * moveSpeed);
  34.        
  35.         JumpCheck();
  36.     }
  37.     void JumpCheck()
  38.     {
  39.         GroundedCheck();
  40.         if (Input.GetKey(KeyCode.Space) && isGrounded && hasWaitedForJump)
  41.         {
  42.             localPlayerRB.AddForce(0, jumpHeight * 10, 0);
  43.             StartCoroutine(JumpDelay());
  44.         }
  45.     }
  46.     void GroundedCheck()
  47.     {
  48.         //Checks the Ray is touching (***anything***)(might want to change later). Determines isGrounded (bool)
  49.         if (Physics.Raycast(this.gameObject.transform.position, -transform.up, playerDistFromGround/10))
  50.         {
  51.             isGrounded = true;
  52.             Debug.DrawRay(this.gameObject.transform.position, -transform.up * playerDistFromGround/10, Color.green, 1f);
  53.         }
  54.         else
  55.         {
  56.             isGrounded = false;
  57.             Debug.DrawRay(this.gameObject.transform.position, -transform.up * playerDistFromGround/10, Color.red, 1f);
  58.         }
  59.     }
  60.     IEnumerator JumpDelay()
  61.     {
  62.         // Delays the time until next jump. Allows for a "feathered" landing and a higher level of agility
  63.         hasWaitedForJump = false;
  64.         yield return new WaitForSeconds(jumpFeatherDelay);
  65.         hasWaitedForJump = true;
  66.     }
  67.    
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement