Advertisement
Guest User

Not Working Movement

a guest
Feb 24th, 2014
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.49 KB | None | 0 0
  1. //Programmer: Kevin Soares
  2. // ©2014 Pytheria Interactive
  3.  
  4. using UnityEngine;
  5. using System.Collections;
  6.  
  7. public class PawnMovement : MonoBehaviour
  8. {
  9.     //Classes
  10.     public PawnState CurrentState = PawnState.Idle;
  11.     public MovementProperties Movement;
  12.     public AnimationProperties Anim;
  13.  
  14.     public void Start()
  15.     {
  16.         Movement.mController = GetComponent<CharacterController> (); //Grab character controller
  17.         Movement.mCurrentSpeed = Movement.mWalkSpeed; //Set speed
  18.     }
  19.  
  20.     public void Update()
  21.     {
  22.     }
  23.  
  24.     public void FixedUpdate()
  25.     {
  26.         MovementController (); //Run movement controller function
  27.         InputController (); //Run input controller function
  28.         AnimationController (); //Run animation controller function
  29.     }
  30.  
  31.     public void MovementController()
  32.     {
  33.         if (Movement.mController.isGrounded)
  34.         {
  35.             Movement.mMoveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical")); //Sent input down to mMoveDirection as a vector3
  36.             Movement.mMoveDirection = transform.TransformDirection(Movement.mMoveDirection); //Apply the movedirection to the transform
  37.             Movement.mMoveDirection *= Movement.mCurrentSpeed; //Now apply speed to the move direction
  38.         }
  39.         Movement.mMoveDirection.y -= Movement.mGravity * Time.deltaTime; //Set gravity
  40.         Movement.mController.Move (Movement.mMoveDirection * Time.deltaTime); //Apply Movedirection to the character controlller
  41.    
  42.    
  43.         if(Movement.mMoveDirection != Vector3.zero)
  44.         {
  45.             Quaternion Rotation = transform.rotation;
  46.             Rotation.SetLookRotation(Movement.mMoveDirection);
  47.             transform.rotation = Rotation;
  48.         }
  49.     }
  50.  
  51.     public void InputController()
  52.     {
  53.         //Controlling input and assigning the current pawn state
  54.  
  55.         if (Input.GetButton ("Run"))
  56.         {
  57.             CurrentState = PawnState.Running; //Change state to running
  58.             Movement.mCurrentSpeed = Movement.mRunSpeed; //Apply running speed
  59.         }
  60.         else if (Input.GetButton ("Jump"))
  61.         {
  62.             CurrentState = PawnState.Jumping; //Change state to jumping
  63.         }
  64.         else if(Movement.mMoveDirection.x != 0 || Movement.mMoveDirection.z != 0)
  65.         {
  66.             CurrentState = PawnState.Walking; //Change state to walking
  67.             Movement.mCurrentSpeed = Movement.mWalkSpeed; //Apply WalkSpeed
  68.         }
  69.         else
  70.         {
  71.             CurrentState = PawnState.Idle; //Change state to idle
  72.         }
  73.     }
  74.  
  75.     public void AnimationController()
  76.     {
  77.         //Controll all animations based on the pawns current state
  78.  
  79.         if(CurrentState == PawnState.Idle)
  80.         {
  81.             Movement.mAnimationHolder.animation.Play(Anim.mIdleAnimation); //Play Idle Animation
  82.         }
  83.         else if(CurrentState == PawnState.Walking)
  84.         {
  85.             Movement.mAnimationHolder.animation.CrossFade(Anim.mWalkAnimation, 0.3f); //Play Walk Animation
  86.         }
  87.         else if(CurrentState == PawnState.Running)
  88.         {
  89.             Movement.mAnimationHolder.animation.CrossFade(Anim.mRunAnimation, 0.3f); //Play Running Animation
  90.         }
  91.         else if(CurrentState == PawnState.Jumping)
  92.         {
  93.             Movement.mAnimationHolder.animation.CrossFade(Anim.mJumpAnimation, 0.3f); //Play Jumping Animation
  94.         }
  95.     }
  96. }
  97.  
  98. [System.Serializable]
  99. public class MovementProperties
  100. {
  101.     public CharacterController mController;
  102.     public GameObject mAnimationHolder;
  103.     public Vector3 mMoveDirection = Vector3.zero;
  104.     public float mCurrentSpeed;
  105.     public float mWalkSpeed;
  106.     public float mRunSpeed;
  107.     public float mGravity;
  108. }
  109.  
  110. [System.Serializable]
  111. public class AnimationProperties
  112. {
  113.     public string mIdleAnimation;
  114.     public string mWalkAnimation;
  115.     public string mRunAnimation;
  116.     public string mJumpAnimation;
  117. }
  118.  
  119. public enum PawnState
  120. {
  121.     Idle,
  122.     Walking,
  123.     Running,
  124.     Jumping
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement