//Run every frame we are in the idle state. void Idle_StateUpdate() { if ((characterUnit.MyCharacter.MyCharacterController as PlayerController).allowedInput && (characterUnit.MyCharacter.MyCharacterController as PlayerController).inputJump) { currentState = AnyRPGCharacterState.Jump; rpgCharacterState = AnyRPGCharacterState.Jump; return; } if (!MaintainingGround()) { currentState = AnyRPGCharacterState.Fall; rpgCharacterState = AnyRPGCharacterState.Fall; return; } if (((characterUnit.MyCharacter.MyCharacterController as PlayerController).HasMoveInput() || (characterUnit.MyCharacter.MyCharacterController as PlayerController).HasTurnInput()) && (characterUnit.MyCharacter.MyCharacterController as PlayerController).canMove) { currentState = AnyRPGCharacterState.Move; rpgCharacterState = AnyRPGCharacterState.Move; return; } // factor in slightly uneven ground which gravity will cause the unit to slide on even when standing still with position and rotation locked // DETECT SUPER LOW RIGIDBODY VELOCITY AND FREEZE CHARACTER if (Mathf.Abs(characterUnit.MyRigidBody.velocity.y) < 0.01 && MaintainingGround() == true) { currentMoveVelocity = new Vector3(0, 0, 0); // disable gravity while this close to the ground so we don't slide down slight inclines characterUnit.MyRigidBody.constraints = RigidbodyConstraints.FreezeAll; } else { // allow the character to fall until they reach the ground characterUnit.MyRigidBody.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ; currentMoveVelocity = new Vector3(0, Mathf.Clamp(characterUnit.MyRigidBody.velocity.y, -53, 0), 0); } }
void Jump_StateUpdate() { // new code to allow bouncing off walls instead of getting stuck flying into them //currentMoveVelocity = CharacterRelativeInput(transform.InverseTransformDirection(animatedUnit.MyRigidBody.velocity)); Vector3 airForwardVelocity = Quaternion.LookRotation(airForwardDirection, Vector3.up) * animatedUnit.MyRigidBody.velocity; currentMoveVelocity = transform.InverseTransformDirection(animatedUnit.MyRigidBody.velocity); Vector3 fromtoMoveVelocity = Quaternion.FromToRotation(airForwardDirection, transform.forward) * transform.InverseTransformDirection(animatedUnit.MyRigidBody.velocity); currentMoveVelocity = fromtoMoveVelocity; if (AcquiringGround() && animatedUnit.MyRigidBody.velocity.y <= 0f && Time.frameCount > lastJumpFrame + 2) { if (((characterUnit.MyCharacter.MyCharacterController as PlayerController).HasMoveInput() || (characterUnit.MyCharacter.MyCharacterController as PlayerController).HasTurnInput()) && (characterUnit.MyCharacter.MyCharacterController as PlayerController).canMove) { // new code to allow not freezing up when landing - fix, should be fall or somehow prevent from getting into move during takeoff //Debug.Log("Jump_StateUpdate() : Entering movement state on frame: " + Time.frameCount + "; Jumped: " + lastJumpFrame); currentState = AnyRPGCharacterState.Move; rpgCharacterState = AnyRPGCharacterState.Move; return; } currentState = AnyRPGCharacterState.Idle; rpgCharacterState = AnyRPGCharacterState.Idle; return; } }
void Move_StateUpdate() { airForwardDirection = transform.forward; airRotation = transform.rotation; if ((characterUnit.MyCharacter.MyCharacterController as PlayerController).allowedInput && (characterUnit.MyCharacter.MyCharacterController as PlayerController).inputJump) { currentState = AnyRPGCharacterState.Jump; rpgCharacterState = AnyRPGCharacterState.Jump; return; } if (!MaintainingGround()) { currentState = AnyRPGCharacterState.Fall; rpgCharacterState = AnyRPGCharacterState.Fall; return; } //Set speed determined by movement type. // since we are in the move state, reset velocity to zero so we can pick up the new values // allow falling while moving by clamping existing y velocity currentMoveVelocity = new Vector3(0, Mathf.Clamp(animatedUnit.MyRigidBody.velocity.y, -53, 0), 0); if (((characterUnit.MyCharacter.MyCharacterController as PlayerController).HasMoveInput() || (characterUnit.MyCharacter.MyCharacterController as PlayerController).HasTurnInput()) && (characterUnit.MyCharacter.MyCharacterController as PlayerController).canMove) { // set clampValue to default of max movement speed float clampValue = PlayerManager.MyInstance.MyMaxMovementSpeed; // set a clamp value to limit movement speed to walking if going backward /* * if (currentMoveVelocity.z < 0) { * clampValue = 1; * } */ // get current movement speed and clamp it to current clamp value float calculatedSpeed = PlayerManager.MyInstance.MyCharacter.MyCharacterController.MyMovementSpeed; calculatedSpeed = Mathf.Clamp(calculatedSpeed, 0, clampValue); if ((characterUnit.MyCharacter.MyCharacterController as PlayerController).HasMoveInput()) { // multiply normalized movement by calculated speed to get actual movement currentMoveVelocity = LocalMovement() * calculatedSpeed; } if ((characterUnit.MyCharacter.MyCharacterController as PlayerController).HasTurnInput()) { currentTurnVelocity = (characterUnit.MyCharacter.MyCharacterController as PlayerController).TurnInput * ((PlayerPrefs.GetFloat("KeyboardTurnSpeed") * 5) + 6.0f); } } else { currentTurnVelocity = Vector3.zero; currentState = AnyRPGCharacterState.Idle; rpgCharacterState = AnyRPGCharacterState.Idle; return; } }
public void ConfigureStateMachine() { //Debug.Log(gameObject.name + ".PlayerUnitMovementController.ConfigureStateMachine()"); currentState = AnyRPGCharacterState.Idle; rpgCharacterState = AnyRPGCharacterState.Idle; if (anyRPGCharacterController != null) { anyRPGCharacterController.OrchestrateStartup(); } }
void Awake() { anyRPGCharacterController = GetComponent <AnyRPGCharacterController>(); characterUnit = GetComponent <CharacterUnit>(); //Set currentState to idle on startup. currentState = AnyRPGCharacterState.Idle; rpgCharacterState = AnyRPGCharacterState.Idle; airForwardDirection = transform.forward; }
void Fall_StateUpdate() { // new code to allow bouncing off walls instead of getting stuck flying into them Vector3 fromtoMoveVelocity = Quaternion.FromToRotation(airForwardDirection, transform.forward) * transform.InverseTransformDirection(animatedUnit.MyRigidBody.velocity); //currentMoveVelocity = transform.InverseTransformDirection(CharacterRelativeInput(animatedUnit.MyRigidBody.velocity)); currentMoveVelocity = new Vector3(fromtoMoveVelocity.x, Mathf.Clamp(fromtoMoveVelocity.y, -53, 0), fromtoMoveVelocity.z); //currentMoveVelocity = new Vector3(currentMoveVelocity.x, 0, currentMoveVelocity.z); if (AcquiringGround()) { if (((characterUnit.MyCharacter.MyCharacterController as PlayerController).HasMoveInput() || (characterUnit.MyCharacter.MyCharacterController as PlayerController).HasTurnInput()) && (characterUnit.MyCharacter.MyCharacterController as PlayerController).canMove) { // new code to allow not freezing up when landing currentState = AnyRPGCharacterState.Move; rpgCharacterState = AnyRPGCharacterState.Move; return; } currentState = AnyRPGCharacterState.Idle; rpgCharacterState = AnyRPGCharacterState.Idle; return; } }
void Jump_StateUpdate() { // new code to allow bouncing off walls instead of getting stuck flying into them //currentMoveVelocity = CharacterRelativeInput(transform.InverseTransformDirection(characterUnit.MyRigidBody.velocity)); Vector3 airForwardVelocity = Quaternion.LookRotation(airForwardDirection, Vector3.up) * characterUnit.MyRigidBody.velocity; currentMoveVelocity = transform.InverseTransformDirection(characterUnit.MyRigidBody.velocity); Vector3 fromtoMoveVelocity = Quaternion.FromToRotation(airForwardDirection, transform.forward) * transform.InverseTransformDirection(characterUnit.MyRigidBody.velocity); currentMoveVelocity = fromtoMoveVelocity; if (AcquiringGround() && characterUnit.MyRigidBody.velocity.y < 0.1) { if (((characterUnit.MyCharacter.MyCharacterController as PlayerController).HasMoveInput() || (characterUnit.MyCharacter.MyCharacterController as PlayerController).HasTurnInput()) && (characterUnit.MyCharacter.MyCharacterController as PlayerController).canMove) { // new code to allow not freezing up when landing currentState = AnyRPGCharacterState.Move; rpgCharacterState = AnyRPGCharacterState.Move; return; } currentState = AnyRPGCharacterState.Idle; rpgCharacterState = AnyRPGCharacterState.Idle; return; } }
public void KnockBack() { //Debug.Log("Knockback()"); currentState = AnyRPGCharacterState.Knockback; rpgCharacterState = AnyRPGCharacterState.Knockback; }