Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Programmer: Kevin Soares
- // ©2014 Pytheria Interactive
- using UnityEngine;
- using System.Collections;
- public class PawnMovement : MonoBehaviour
- {
- //Classes
- public PawnState CurrentState = PawnState.Idle;
- public MovementProperties Movement;
- public AnimationProperties Anim;
- public void Start()
- {
- Movement.mController = GetComponent<CharacterController> (); //Grab character controller
- Movement.mCurrentSpeed = Movement.mWalkSpeed; //Set speed
- }
- public void Update()
- {
- }
- public void FixedUpdate()
- {
- MovementController (); //Run movement controller function
- InputController (); //Run input controller function
- AnimationController (); //Run animation controller function
- }
- public void MovementController()
- {
- if (Movement.mController.isGrounded)
- {
- Movement.mMoveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical")); //Sent input down to mMoveDirection as a vector3
- Movement.mMoveDirection = transform.TransformDirection(Movement.mMoveDirection); //Apply the movedirection to the transform
- Movement.mMoveDirection *= Movement.mCurrentSpeed; //Now apply speed to the move direction
- }
- Movement.mMoveDirection.y -= Movement.mGravity * Time.deltaTime; //Set gravity
- Movement.mController.Move (Movement.mMoveDirection * Time.deltaTime); //Apply Movedirection to the character controlller
- if(Movement.mMoveDirection != Vector3.zero)
- {
- Quaternion Rotation = transform.rotation;
- Rotation.SetLookRotation(Movement.mMoveDirection);
- transform.rotation = Rotation;
- }
- }
- public void InputController()
- {
- //Controlling input and assigning the current pawn state
- if (Input.GetButton ("Run"))
- {
- CurrentState = PawnState.Running; //Change state to running
- Movement.mCurrentSpeed = Movement.mRunSpeed; //Apply running speed
- }
- else if (Input.GetButton ("Jump"))
- {
- CurrentState = PawnState.Jumping; //Change state to jumping
- }
- else if(Movement.mMoveDirection.x != 0 || Movement.mMoveDirection.z != 0)
- {
- CurrentState = PawnState.Walking; //Change state to walking
- Movement.mCurrentSpeed = Movement.mWalkSpeed; //Apply WalkSpeed
- }
- else
- {
- CurrentState = PawnState.Idle; //Change state to idle
- }
- }
- public void AnimationController()
- {
- //Controll all animations based on the pawns current state
- if(CurrentState == PawnState.Idle)
- {
- Movement.mAnimationHolder.animation.Play(Anim.mIdleAnimation); //Play Idle Animation
- }
- else if(CurrentState == PawnState.Walking)
- {
- Movement.mAnimationHolder.animation.CrossFade(Anim.mWalkAnimation, 0.3f); //Play Walk Animation
- }
- else if(CurrentState == PawnState.Running)
- {
- Movement.mAnimationHolder.animation.CrossFade(Anim.mRunAnimation, 0.3f); //Play Running Animation
- }
- else if(CurrentState == PawnState.Jumping)
- {
- Movement.mAnimationHolder.animation.CrossFade(Anim.mJumpAnimation, 0.3f); //Play Jumping Animation
- }
- }
- }
- [System.Serializable]
- public class MovementProperties
- {
- public CharacterController mController;
- public GameObject mAnimationHolder;
- public Vector3 mMoveDirection = Vector3.zero;
- public float mCurrentSpeed;
- public float mWalkSpeed;
- public float mRunSpeed;
- public float mGravity;
- }
- [System.Serializable]
- public class AnimationProperties
- {
- public string mIdleAnimation;
- public string mWalkAnimation;
- public string mRunAnimation;
- public string mJumpAnimation;
- }
- public enum PawnState
- {
- Idle,
- Walking,
- Running,
- Jumping
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement