Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- // The line below automatically applies a SuperCharacterController to
- // the object if it doesn't have it
- [RequireComponent(typeof(SuperCharacterController))]
- public class PlayerMachine : SuperStateMachine {
- public float speed = 6.0F;
- public float jumpSpeed = 100.0F;
- public float gravity = 20.0F;
- private Vector3 moveDirection = Vector3.zero;
- float hori;
- float vert;
- float dist;
- bool jump;
- // Add more states by comma separating them
- enum PlayerStates { Idle, Jumping, Falling, Running }
- private SuperCharacterController controller;
- void Start () {
- // Put any code here you want to run ONCE, when the object is initialized
- // Grab the controller object from our object
- controller = gameObject.GetComponent<SuperCharacterController>();
- // Set our currentState to idle on startup
- currentState = PlayerStates.Idle;
- }
- protected override void EarlyGlobalSuperUpdate()
- {
- // Put any code in here you want to run BEFORE the state's update function.
- // This is run regardless of what state you're in
- hori = Input.GetAxis("Horizontal");
- vert = Input.GetAxis("Vertical");
- jump = Input.GetButton("Jump");
- dist = controller.currentGround.Hit.distance;
- }
- protected override void LateGlobalSuperUpdate()
- {
- // Put any code in here you want to run AFTER the state's update function.
- // This is run regardless of what state you're in
- }
- /*void Update () {
- * Update is normally run once on every frame update. We won't be using it
- * in this case, since the SuperCharacterController component sends a callback Update
- * called SuperUpdate. SuperUpdate is recieved by the SuperStateMachine, and then fires
- * further callbacks depending on the state
- }*/
- // Below are the three state functions. Each one is called based on the name of the state,
- // so when currentState = Idle, we call Idle_EnterState. If currentState = Jump, we call
- // Jump_SuperUpdate()
- void Idle_EnterState()
- {
- // Run once when we enter the idle state
- controller.EnableClamping();
- }
- void Idle_SuperUpdate()
- {
- // Run every frame we are in the idle state
- transform.position = new Vector3(0, 0, 0);
- }
- void Idle_ExitState()
- {
- // Run once when we exit the idle state
- }
- void Jumping_EnterState()
- {
- // Run once when we enter the idle state
- controller.DisableClamping();
- }
- void Jumping_SuperUpdate()
- {
- // Run every frame we are in the idle state
- }
- void Jumping_ExitState()
- {
- // Run once when we exit the idle state
- }
- void Falling_EnterState()
- {
- // Run once when we enter the idle state
- controller.DisableClamping();
- }
- void Falling_SuperUpdate()
- {
- // Run every frame we are in the idle state
- }
- void Falling_ExitState()
- {
- // Run once when we exit the idle state
- }
- void Running_EnterState()
- {
- // Run once when we enter the running state
- controller.EnableClamping();
- }
- void Running_SuperUpdate()
- {
- // Run every frame we are in the running state
- //moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
- //moveDirection = transform.TransformDirection(moveDirection);
- //moveDirection *= speed;
- Vector3 moveForward = new Vector3(0, 0, Input.GetAxis("Vertical"));
- transform.position += moveForward;
- if (jump) // Jumping is true
- {
- currentState = PlayerStates.Jumping;
- }
- if (hori != 0f && dist <= 0.05 || vert != 0f && dist <= 0.05) // Movement in a direction
- {
- currentState = PlayerStates.Running;
- }
- }
- void Running_ExitState()
- {
- // Run once when we exit the running state
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement