Advertisement
mrslayer01

NinjaGame Part 1

Sep 19th, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. public class PlayerMovement : MonoBehaviour {
  2.     public float speed;
  3.     float jumpHeight = 8f;
  4.  
  5.     //Players Movement
  6.     public Vector3 direction = Vector3.zero;
  7.     float verticalVeolocity = 0;
  8.     //End Player Movement
  9.  
  10.     //Components
  11.     CharacterController cc;
  12.     //End Components
  13.  
  14.     void Awake() {
  15.         speed = 8;
  16.     }
  17.  
  18.     void Start () {
  19.         //Components
  20.         cc = GetComponent<CharacterController> ();
  21.     }
  22.  
  23.     void Update () {
  24.         //The players direction using AD, left arrow and right arrow.arrow.
  25.         direction = transform.rotation * new Vector3 (Input.GetAxisRaw ("Horizontal"), 0, 0);
  26.  
  27.         //If the Character Controller is grounded then allow the player to Jump.
  28.         if(cc.isGrounded && Input.GetButton("Jump")) {
  29.             //WQhen ever the player jumps we want to set his verticalVeolocity to jumpHeight.
  30.             verticalVeolocity = jumpHeight;
  31.         }
  32.     }
  33.  
  34.     void FixedUpdate() {
  35.         Vector3 dist = direction * speed * Time.deltaTime;
  36.         //Jumping
  37.         if (cc.isGrounded && verticalVeolocity < 0) {
  38.             verticalVeolocity = Physics.gravity.y * Time.deltaTime;
  39.         } else {
  40.             if(Mathf.Abs (verticalVeolocity) > jumpHeight * 0.75) {
  41.                 //Put animation for jumping here!
  42.             }
  43.             //Apply Gravity
  44.             verticalVeolocity += Physics.gravity.y * Time.deltaTime;
  45.         }
  46.  
  47.         //Add the verticalVeolocity to our actual movement this frame.
  48.         dist.y = verticalVeolocity * Time.deltaTime;
  49.  
  50.         //Apply the movement to the CC.
  51.         cc.Move (dist);
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement