Advertisement
BigRob154

Unity 5 Player Movement

Aug 4th, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.00 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [RequireComponent(typeof(CharacterController))]
  5. public class PlayerMovement : MonoBehaviour {
  6.  
  7.     private CharacterController charCtrl;
  8.     private Vector3 moveDirection = Vector3.zero;
  9.     private bool hasJumped = false;
  10.  
  11.     public float MoveSpeed = 6f;
  12.     public float JumpSpeed = 20f;
  13.     public float Gravity = 2f;
  14.    
  15.     void Start() {
  16.         charCtrl = GetComponent<CharacterController>();
  17.     }
  18.  
  19.     void Update() {
  20.         if (charCtrl.isGrounded) {
  21.             moveDirection.x = Input.GetAxisRaw("Horizontal");
  22.             moveDirection.y = 0f;
  23.             moveDirection.z = Input.GetAxisRaw("Vertical");
  24.             moveDirection = transform.TransformDirection(moveDirection);
  25.             moveDirection.Normalize();
  26.             moveDirection *= MoveSpeed;
  27.             if (Input.GetButton("Jump")) {
  28.                 if (!hasJumped) {
  29.                     hasJumped = true;
  30.                     moveDirection.y = JumpSpeed;
  31.                 }
  32.             } else if (hasJumped) {
  33.                 hasJumped = false;
  34.             }
  35.         }
  36.         moveDirection.y += Gravity;
  37.         charCtrl.Move(moveDirection * Time.deltaTime);
  38.     }
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement