Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class StandardMovement : MonoBehaviour {
- //X & Z Movement
- public int moveSpeed;
- public int jumpHeight;
- float moveHor;
- float moveVer;
- Rigidbody localPlayerRB;
- //Y Movement
- bool isGrounded;
- bool hasWaitedForJump = true;
- public float playerDistFromGround;
- public float jumpFeatherDelay;
- void Start () {
- localPlayerRB = GetComponent<Rigidbody>();
- }
- void FixedUpdate () {
- //Checks for input
- moveHor = Input.GetAxis("Horizontal");
- moveVer = Input.GetAxis("Vertical");
- //Neutralizes moveY. (We dont want X & Z movement to effect the Y velocity
- float moveY = localPlayerRB.velocity.y;
- //Updates the RB velocity according to Player input
- localPlayerRB.velocity = new Vector3(moveHor * moveSpeed, moveY, moveVer * moveSpeed);
- JumpCheck();
- }
- void JumpCheck()
- {
- GroundedCheck();
- if (Input.GetKey(KeyCode.Space) && isGrounded && hasWaitedForJump)
- {
- localPlayerRB.AddForce(0, jumpHeight * 10, 0);
- StartCoroutine(JumpDelay());
- }
- }
- void GroundedCheck()
- {
- //Checks the Ray is touching (***anything***)(might want to change later). Determines isGrounded (bool)
- if (Physics.Raycast(this.gameObject.transform.position, -transform.up, playerDistFromGround/10))
- {
- isGrounded = true;
- Debug.DrawRay(this.gameObject.transform.position, -transform.up * playerDistFromGround/10, Color.green, 1f);
- }
- else
- {
- isGrounded = false;
- Debug.DrawRay(this.gameObject.transform.position, -transform.up * playerDistFromGround/10, Color.red, 1f);
- }
- }
- IEnumerator JumpDelay()
- {
- // Delays the time until next jump. Allows for a "feathered" landing and a higher level of agility
- hasWaitedForJump = false;
- yield return new WaitForSeconds(jumpFeatherDelay);
- hasWaitedForJump = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement