mirror of
https://github.com/Leahnaya/TheKingsRace.git
synced 2026-08-21 09:54:28 -05:00
Have a lot of the basic movement in the stateMachine
Will need to finish up and implement some of the ragdoll interactions
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d141d72bd4c482a44b53b7b7681bf510
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,31 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class AerialFallingState : AerialBaseState
|
||||
{
|
||||
public override void EnterState(AerialStateManager aSM){
|
||||
Debug.Log("Falling State");
|
||||
}
|
||||
|
||||
public override void UpdateState(AerialStateManager aSM){
|
||||
if(aSM.pStats.GravVel > 0){
|
||||
aSM.SwitchState(aSM.JumpingState);
|
||||
}
|
||||
else if(Input.GetButton("Jump") && aSM.pStats.HasGlider){
|
||||
aSM.SwitchState(aSM.GlidingState);
|
||||
}
|
||||
|
||||
if(aSM.isGrounded){
|
||||
aSM.SwitchState(aSM.GroundedState);
|
||||
}
|
||||
}
|
||||
|
||||
public override void FixedUpdateState(AerialStateManager aSM){
|
||||
aSM.GravityCalculation(aSM.pStats.PlayerGrav);
|
||||
}
|
||||
|
||||
public override void OnCollisionEnter(AerialStateManager aSM){
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 61783f049a8966743ae46c39eeec1881
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,35 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class AerialGlidingState : AerialBaseState
|
||||
{
|
||||
float tempTraction;
|
||||
|
||||
public override void EnterState(AerialStateManager aSM){
|
||||
Debug.Log("Gliding State");
|
||||
|
||||
tempTraction = aSM.pStats.Traction;
|
||||
aSM.pStats.Traction = 1.0f;
|
||||
}
|
||||
|
||||
public override void UpdateState(AerialStateManager aSM){
|
||||
if(!Input.GetButton("Jump")){
|
||||
aSM.pStats.Traction = tempTraction;
|
||||
aSM.SwitchState(aSM.FallingState);
|
||||
}
|
||||
|
||||
if(aSM.isGrounded){
|
||||
aSM.pStats.Traction = tempTraction;
|
||||
aSM.SwitchState(aSM.GroundedState);
|
||||
}
|
||||
}
|
||||
|
||||
public override void FixedUpdateState(AerialStateManager aSM){
|
||||
aSM.GravityCalculation(9);
|
||||
}
|
||||
|
||||
public override void OnCollisionEnter(AerialStateManager aSM){
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c12fc028d5f2f184584d2eca49241719
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,27 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class AerialGroundedState : AerialBaseState
|
||||
{
|
||||
public override void EnterState(AerialStateManager aSM){
|
||||
Debug.Log("Grounded State");
|
||||
}
|
||||
|
||||
public override void UpdateState(AerialStateManager aSM){
|
||||
if(aSM.pStats.GravVel < 0){
|
||||
aSM.SwitchState(aSM.FallingState);
|
||||
}
|
||||
else if(aSM.pStats.GravVel > 0){
|
||||
aSM.SwitchState(aSM.JumpingState);
|
||||
}
|
||||
}
|
||||
|
||||
public override void FixedUpdateState(AerialStateManager aSM){
|
||||
aSM.GravityCalculation(aSM.pStats.PlayerGrav);
|
||||
}
|
||||
|
||||
public override void OnCollisionEnter(AerialStateManager aSM){
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f8adbd7189ac5324ea70d17e1a7d80eb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,30 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class AerialJumpingState : AerialBaseState
|
||||
{
|
||||
|
||||
public override void EnterState(AerialStateManager aSM){
|
||||
Debug.Log("Jumping State");
|
||||
}
|
||||
|
||||
public override void UpdateState(AerialStateManager aSM){
|
||||
|
||||
if(aSM.pStats.GravVel < 0){
|
||||
aSM.SwitchState(aSM.FallingState);
|
||||
}
|
||||
|
||||
if(aSM.isGrounded){
|
||||
aSM.SwitchState(aSM.GroundedState);
|
||||
}
|
||||
}
|
||||
|
||||
public override void FixedUpdateState(AerialStateManager aSM){
|
||||
aSM.GravityCalculation(aSM.pStats.PlayerGrav);
|
||||
}
|
||||
|
||||
public override void OnCollisionEnter(AerialStateManager aSM){
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5dc434010f70e6846821781075391a89
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public abstract class AerialBaseState
|
||||
{
|
||||
public abstract void EnterState(AerialStateManager aSM);
|
||||
public abstract void UpdateState(AerialStateManager aSM);
|
||||
public abstract void FixedUpdateState(AerialStateManager aSM);
|
||||
public abstract void OnCollisionEnter(AerialStateManager aSM);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f3d9c78aa56075f4db2794b22eafce33
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,228 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class AerialStateManager : MonoBehaviour
|
||||
{
|
||||
////Player States
|
||||
AerialBaseState currentState;
|
||||
public AerialBaseState previousState;
|
||||
|
||||
//Aerial States
|
||||
public AerialFallingState FallingState = new AerialFallingState();
|
||||
public AerialGlidingState GlidingState = new AerialGlidingState();
|
||||
public AerialGroundedState GroundedState = new AerialGroundedState();
|
||||
public AerialJumpingState JumpingState = new AerialJumpingState();
|
||||
////
|
||||
|
||||
////Objects Sections
|
||||
private GameObject parentObj; // Parent object
|
||||
public Camera cam; // Camera object
|
||||
////
|
||||
|
||||
////Components Section
|
||||
public CharacterController moveController; // Character Controller
|
||||
private Rigidbody rB; // Players Rigidbody
|
||||
private CapsuleCollider capCol; // Players Capsule Collider
|
||||
private Animator animator; // Animation Controller
|
||||
////
|
||||
|
||||
////Scripts Section
|
||||
public PlayerStats pStats; // Player Stats
|
||||
public MoveStateManager mSM;
|
||||
////
|
||||
|
||||
////Variables Section
|
||||
//Jump Variables
|
||||
public int curJumpNum; // current Jumps Used
|
||||
public bool jumpHeld; // Jump is Held
|
||||
private bool jumpPressed; // Jamp was pressed
|
||||
public float coyJumpTimer = 0.1f; // Default Coyote Jump time
|
||||
public float curCoyJumpTimer = 0.1f; // current Coyote Jump time
|
||||
public float lowJumpMultiplier; // Short jump multiplier
|
||||
public float fallMultiplier; // High Jump Multiplier
|
||||
|
||||
//Gravity Variables//
|
||||
private float maxG = -100; // max downwards velocity
|
||||
|
||||
//Ground Check
|
||||
public bool isGrounded; // is player grounded
|
||||
public float groundCheckDistance = 0.05f; // offset distance to check ground
|
||||
private const float jumpGroundingPreventionTime = 0.2f; // delay so player doesn't get snapped to ground while jumping
|
||||
private const float groundCheckDistanceInAir = 0.07f; // How close we have to get to ground to start checking for grounded again
|
||||
private Ray groundRay; // ground ray
|
||||
private RaycastHit groundHit; // ground raycast
|
||||
////
|
||||
|
||||
void Awake(){
|
||||
|
||||
////Initialize Player Components
|
||||
moveController = GetComponent<CharacterController>(); // set Character Controller
|
||||
rB = GetComponent<Rigidbody>(); //set Rigid Body
|
||||
capCol = GetComponent<CapsuleCollider>(); // set Capsule Collider
|
||||
capCol.enabled = true;
|
||||
parentObj = transform.parent.gameObject; // set parent object
|
||||
animator = GetComponent<Animator>(); // set animator
|
||||
////
|
||||
|
||||
////Initialize Scripts
|
||||
pStats = GetComponent<PlayerStats>(); // set PlayerStats
|
||||
mSM = GetComponent<MoveStateManager>();
|
||||
////
|
||||
}
|
||||
|
||||
void Start(){
|
||||
//players starting state
|
||||
currentState = GroundedState;
|
||||
previousState = GroundedState;
|
||||
currentState.EnterState(this);
|
||||
}
|
||||
|
||||
void Update(){
|
||||
//calls any logic in the update state from current state
|
||||
currentState.UpdateState(this);
|
||||
}
|
||||
|
||||
void FixedUpdate(){
|
||||
|
||||
if(moveController.enabled){
|
||||
Jump();
|
||||
GroundCheck();
|
||||
DownwardMovement();
|
||||
}
|
||||
else{
|
||||
Debug.Log(currentState);
|
||||
//Gravity without moveController
|
||||
pStats.GravVel -= pStats.PlayerGrav * Time.deltaTime;
|
||||
rB.AddForce(new Vector3(0,pStats.GravVel,0));
|
||||
}
|
||||
|
||||
|
||||
//calls any logic in the fixed update state from current state
|
||||
currentState.FixedUpdateState(this);
|
||||
}
|
||||
|
||||
public void SwitchState(AerialBaseState state){
|
||||
|
||||
//Sets the previous State
|
||||
previousState = currentState;
|
||||
|
||||
//updates current state and calls logic for entering
|
||||
currentState = state;
|
||||
currentState.EnterState(this);
|
||||
}
|
||||
|
||||
//Uses Given gravity to apply a downwards force while allowing coyote Jump and short hops
|
||||
public void GravityCalculation(float grav){
|
||||
if(moveController.enabled){
|
||||
|
||||
//apply slight upwards force for jump smoothing when g < 0
|
||||
if(pStats.GravVel < 0){
|
||||
pStats.GravVel += grav * (fallMultiplier - 1) * Time.deltaTime;
|
||||
}
|
||||
|
||||
//apply smaller upwards force if jump is released early when jumping creating a short jump
|
||||
else if (pStats.GravVel > 0 && !Input.GetButton("Jump")){
|
||||
pStats.GravVel += grav * (lowJumpMultiplier - 1) * Time.deltaTime;
|
||||
}
|
||||
|
||||
//apply gravity if not grounded and coyote timer is less than 0
|
||||
if((isGrounded == false && curCoyJumpTimer <= 0)/* || grapple.isGrappled*/){
|
||||
pStats.GravVel -= grav * Time.deltaTime;
|
||||
}
|
||||
//else don't apply gravity
|
||||
else{
|
||||
pStats.GravVel = 0;
|
||||
}
|
||||
|
||||
//Caps out the players downwards speed
|
||||
if(pStats.GravVel < maxG){
|
||||
pStats.GravVel = maxG;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Checks if player is grounded
|
||||
void GroundCheck(){
|
||||
// Make sure that the ground check distance while already in air is very small, to prevent suddenly snapping to ground
|
||||
float chosenGroundCheckDistance = isGrounded ? (moveController.skinWidth + groundCheckDistance) : groundCheckDistanceInAir;
|
||||
|
||||
// reset values before the ground check
|
||||
isGrounded = false;
|
||||
groundRay = new Ray(moveController.transform.position, Vector3.down);
|
||||
|
||||
if (Physics.Raycast(groundRay, out groundHit, moveController.height + groundCheckDistance) && !jumpPressed)
|
||||
{
|
||||
// Only consider this a valid ground hit if the ground normal goes in the same direction as the character up
|
||||
if (Vector3.Dot(groundHit.normal, transform.up) > 0f)
|
||||
{
|
||||
isGrounded = true;
|
||||
// handle snapping to the ground
|
||||
if (groundHit.distance > moveController.skinWidth /* && !grapple.isGrappled*/)
|
||||
{
|
||||
moveController.Move(Vector3.down * groundHit.distance);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DownwardMovement(){
|
||||
Vector3 moveY = new Vector3(0,pStats.GravVel,0) * Time.deltaTime;
|
||||
|
||||
moveController.Move(moveY);
|
||||
}
|
||||
|
||||
private void Jump(){
|
||||
//If space/south gamepad button is pressed apply an upwards force to the player
|
||||
if (Input.GetAxis("Jump") != 0 && !jumpHeld && curJumpNum < pStats.JumpNum)
|
||||
{
|
||||
pStats.GravVel = pStats.JumpPow;
|
||||
|
||||
curJumpNum++;
|
||||
jumpHeld = true;
|
||||
jumpPressed = true;
|
||||
}
|
||||
|
||||
//If grounded no jumps have been used and coyote Timer is refreshed
|
||||
if(isGrounded && pStats.GravVel == 0){
|
||||
curCoyJumpTimer = coyJumpTimer;
|
||||
curJumpNum = 0;
|
||||
}
|
||||
//else start the coyote timer
|
||||
else curCoyJumpTimer -= Time.deltaTime;
|
||||
|
||||
//if jump is being held coyote timer is zero
|
||||
if(jumpHeld) curCoyJumpTimer = 0;
|
||||
|
||||
//If space/south face gamepad button isn't being pressed then jump is false
|
||||
if (Input.GetAxis("Jump") == 0){
|
||||
jumpHeld = false;
|
||||
}
|
||||
|
||||
if(pStats.GravVel < 0){
|
||||
jumpPressed = false;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
//Wallrunning
|
||||
else if (pStats.HasWallrun) {
|
||||
//Run wall run script
|
||||
wallRun.WallRunRoutine();
|
||||
|
||||
//if wallrunning apply different gravity
|
||||
if(wallRun.IsWallRunning()){
|
||||
if(wallRun.firstAttach){
|
||||
g = 0;
|
||||
wallRun.firstAttach = false;
|
||||
}
|
||||
GravityCalculation(2);
|
||||
}
|
||||
|
||||
//Normal gravity if not wallrunning
|
||||
else{
|
||||
GravityCalculation(pStats.PlayerGrav);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 201caa2b2f1776a46af6bdfd52456028
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 44ea0e21af7d7024099c4da443b76164
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5149f1c52a18e6a45b82453d3af9a9b9
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,52 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class MoveRagdollState : MoveBaseState
|
||||
{
|
||||
float ragTime;
|
||||
Vector3 prevRot;
|
||||
bool beginRagTimer = false;
|
||||
|
||||
public override void EnterState(MoveStateManager mSM){
|
||||
Debug.Log("Ragdoll State");
|
||||
|
||||
ragTime = mSM.pStats.RecovTime;
|
||||
prevRot = mSM.transform.localEulerAngles;
|
||||
mSM.capCol.enabled = true;
|
||||
mSM.moveController.enabled = false;
|
||||
mSM.rB.isKinematic = false;
|
||||
mSM.rB.detectCollisions = true;
|
||||
|
||||
mSM.rB.AddForce(mSM.dirHit, ForceMode.Impulse);
|
||||
}
|
||||
|
||||
public override void UpdateState(MoveStateManager mSM){
|
||||
if(!beginRagTimer){
|
||||
beginRagTimer = Physics.Raycast(mSM.transform.position, -Vector3.up, mSM.distToGround + 1f);
|
||||
}
|
||||
else{
|
||||
ragTime -= Time.deltaTime;
|
||||
}
|
||||
}
|
||||
|
||||
public override void FixedUpdateState(MoveStateManager mSM){
|
||||
//Has to be in Fixed Update because it has player movement
|
||||
if(ragTime <= 0 && beginRagTimer){
|
||||
ragTime = 0;
|
||||
beginRagTimer = false;
|
||||
|
||||
mSM.pStats.GravVel = 50;
|
||||
mSM.capCol.enabled = false;
|
||||
mSM.moveController.enabled = true;
|
||||
mSM.rB.isKinematic = true;
|
||||
mSM.rB.detectCollisions = false;
|
||||
mSM.transform.localEulerAngles = prevRot;
|
||||
mSM.SwitchState(mSM.RecoveringState);
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnCollisionEnter(MoveStateManager mSM){
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b7164475ffa0df6409ecd43b0517e4b0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class MoveRecoveringState : MoveBaseState
|
||||
{
|
||||
////// ADD SOMETHING THAT CHECKS ANIMATION FINISH BEFORE GO TO IDLE
|
||||
|
||||
public override void EnterState(MoveStateManager mSM){
|
||||
mSM.CancelMomentum();
|
||||
}
|
||||
|
||||
public override void UpdateState(MoveStateManager mSM){
|
||||
mSM.SwitchState(mSM.IdleState);
|
||||
}
|
||||
|
||||
public override void FixedUpdateState(MoveStateManager mSM){
|
||||
|
||||
}
|
||||
|
||||
public override void OnCollisionEnter(MoveStateManager mSM){
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dee57766c5fb3c34c8979d3e14d4e94e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -4,23 +4,116 @@ using UnityEngine;
|
||||
|
||||
public class MoveStateManager : MonoBehaviour
|
||||
{
|
||||
////Player States
|
||||
MoveBaseState currentState;
|
||||
MoveIdleState IdleState = new MoveIdleState();
|
||||
MoveWalkState WalkState = new MoveWalkState();
|
||||
MoveJogState JogState = new MoveJogState();
|
||||
MoveRunState RunState = new MoveRunState();
|
||||
public MoveBaseState previousState;
|
||||
|
||||
//WASD States
|
||||
public MoveIdleState IdleState = new MoveIdleState();
|
||||
public MoveWalkState WalkState = new MoveWalkState();
|
||||
public MoveJogState JogState = new MoveJogState();
|
||||
public MoveRunState RunState = new MoveRunState();
|
||||
|
||||
//Slide States
|
||||
public MoveSlideState SlideState = new MoveSlideState();
|
||||
public MoveCrouchState CrouchState = new MoveCrouchState();
|
||||
public MoveCrouchWalkState CrouchWalkState = new MoveCrouchWalkState();
|
||||
|
||||
//Incapitated States
|
||||
public MoveRagdollState RagdollState = new MoveRagdollState();
|
||||
public MoveRecoveringState RecoveringState = new MoveRecoveringState();
|
||||
////
|
||||
|
||||
////Objects Sections
|
||||
private GameObject parentObj; // Parent object
|
||||
public Camera cam; // Camera object
|
||||
////
|
||||
|
||||
////Components Section
|
||||
public CharacterController moveController; // Character Controller
|
||||
public Rigidbody rB; // Players Rigidbody
|
||||
public CapsuleCollider capCol; // Players Capsule Collider
|
||||
private Animator animator; // Animation Controller
|
||||
////
|
||||
|
||||
////Scripts Section
|
||||
public PlayerStats pStats; // Player Stats
|
||||
////
|
||||
|
||||
////State Transition Variables
|
||||
public float idleLimit = .3f;
|
||||
public float walkLimit = 10.0f;
|
||||
public float jogLimit = 20f;
|
||||
public float runLimit = 30f;
|
||||
////
|
||||
|
||||
////Player Variables Section
|
||||
//Speed Variables
|
||||
public Vector3 vel; // moveZ + moveX
|
||||
private Vector3 moveZ; // Local Horizontal Vector
|
||||
private Vector3 moveX; // Local Vertical Vector
|
||||
public Vector3 driftVel; // Lerped Movement Vector
|
||||
public float calculatedCurVel; // calculated current vel using driftVel
|
||||
|
||||
//Slide Variables
|
||||
public Vector3 slideUp; // Slide upwards direction
|
||||
|
||||
//Camera Variables
|
||||
private Vector3 camRotation; // cameras camera rotation vector
|
||||
[Range(-45, -15)]
|
||||
public int minAngle = -30; // minimum downwards cam angle
|
||||
[Range(30, 80)]
|
||||
public int maxAngle = 45; // Max upwards cam angle
|
||||
[Range(50, 500)]
|
||||
public int sensitivity = 200; // Camera sensitivity
|
||||
|
||||
//Ragdoll Variables
|
||||
public Vector3 dirHit;
|
||||
public float distToGround; // distance to ground
|
||||
|
||||
//Impact Variables
|
||||
private float mass = 5.0F; // mass variable for Impact
|
||||
private Vector3 impact = Vector3.zero; // Impact Vector
|
||||
////
|
||||
|
||||
|
||||
void Awake(){
|
||||
|
||||
////Initialize Player Components
|
||||
moveController = GetComponent<CharacterController>(); // set Character Controller
|
||||
rB = GetComponent<Rigidbody>(); //set Rigid Body
|
||||
capCol = GetComponent<CapsuleCollider>(); // set Capsule Collider
|
||||
capCol.enabled = true;
|
||||
parentObj = transform.parent.gameObject; // set parent object
|
||||
animator = GetComponent<Animator>(); // set animator
|
||||
////
|
||||
|
||||
////Initialize Scripts
|
||||
pStats = GetComponent<PlayerStats>(); // set PlayerStats
|
||||
////
|
||||
}
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
//players starting state
|
||||
currentState = IdleState;
|
||||
previousState = IdleState;
|
||||
currentState.EnterState(this);
|
||||
|
||||
//Slide Upwards Variable
|
||||
slideUp = GetComponentInParent<Transform>().up; // get parents up direction
|
||||
distToGround = GetComponent<Collider>().bounds.extents.y; // set players distance to ground
|
||||
|
||||
Cursor.lockState = CursorLockMode.Locked; // Lock cursor on start if you are the local player
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
//calculates vel using driftVel will need to be relocated
|
||||
calculatedCurVel = driftVel.magnitude * 50f;
|
||||
|
||||
//calls any logic in the update state from current state
|
||||
currentState.UpdateState(this);
|
||||
}
|
||||
@@ -29,10 +122,132 @@ public class MoveStateManager : MonoBehaviour
|
||||
|
||||
//calls any logic in the fixed update state from current state
|
||||
currentState.FixedUpdateState(this);
|
||||
|
||||
if(cam.enabled) Rotation();
|
||||
else Debug.Log("Cam Disabled");
|
||||
}
|
||||
|
||||
void SwitchState(MoveBaseState state){
|
||||
public void SwitchState(MoveBaseState state){
|
||||
|
||||
//Sets the previous State
|
||||
previousState = currentState;
|
||||
|
||||
//updates current state and calls logic for entering
|
||||
currentState = state;
|
||||
state.EnterState(this);
|
||||
currentState.EnterState(this);
|
||||
}
|
||||
|
||||
|
||||
////Broad Functions
|
||||
//Player Speed Calculator
|
||||
public float PlayerSpeed(){
|
||||
//If nothing is pressed speed is 0
|
||||
if ((Input.GetAxis("Vertical") == 0.0f && Input.GetAxis("Horizontal") == 0.0f))
|
||||
{
|
||||
pStats.CurVel = 0.0f;
|
||||
return pStats.CurVel;
|
||||
}
|
||||
//If current speed is below min when pressed set to minimum speed
|
||||
else if (pStats.CurVel < pStats.MinVel)
|
||||
{
|
||||
pStats.CurVel = pStats.MinVel;
|
||||
return pStats.MinVel;
|
||||
}
|
||||
// while the speed is below max speed slowly increase it
|
||||
else if ((pStats.CurVel >= pStats.MinVel) && (pStats.CurVel < pStats.MaxVel))
|
||||
{
|
||||
pStats.CurVel += pStats.Acc;
|
||||
return pStats.CurVel;
|
||||
}
|
||||
//If the players speed is above or equal to max speed set speed to max
|
||||
else if (pStats.CurVel >= pStats.MaxVel)
|
||||
{
|
||||
pStats.CurVel = pStats.MaxVel;
|
||||
return pStats.CurVel;
|
||||
|
||||
}
|
||||
//case if somehow they escape this check
|
||||
else{
|
||||
Debug.Log("Something has gone wrong with the PlayerSpeed()");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
//Wasd movement using player speed
|
||||
public void DirectionalMovement(){
|
||||
//Keyboard inputs
|
||||
//Checks if movement keys have been pressed and calculates correct vector
|
||||
moveX = transform.right * Input.GetAxis("Horizontal") * Time.deltaTime * PlayerSpeed();
|
||||
moveZ = transform.forward * Input.GetAxis("Vertical") * Time.deltaTime * PlayerSpeed();
|
||||
|
||||
vel = moveX + moveZ;
|
||||
Vector3 moveXZ = new Vector3(vel.x, 0, vel.z);
|
||||
driftVel = Vector3.Lerp(driftVel, moveXZ, pStats.Traction * Time.deltaTime);
|
||||
|
||||
//Actually move he player
|
||||
moveController.Move(driftVel);
|
||||
}
|
||||
|
||||
//Slide movement
|
||||
public void SlideMovement(){
|
||||
|
||||
moveX = Vector3.zero;
|
||||
moveZ = Vector3.zero;
|
||||
|
||||
//Adds vectors based on movement keys and other conditions to check what the
|
||||
//player vector should be under the circumstances
|
||||
vel = moveX + moveZ;
|
||||
Vector3 moveXZ = new Vector3(vel.x, 0, vel.z);
|
||||
driftVel = Vector3.Lerp(driftVel, moveXZ, pStats.Traction * Time.deltaTime);
|
||||
|
||||
//Actually move he player
|
||||
moveController.Move(driftVel);
|
||||
}
|
||||
|
||||
//Camera and player rotation
|
||||
private void Rotation(){
|
||||
//If moveController is enabled allow Camera control
|
||||
if(moveController.enabled){
|
||||
//if input is received from Mouse X
|
||||
if (Input.GetAxis("Mouse X") != 0){
|
||||
transform.parent.Rotate(Vector3.up * sensitivity * Time.deltaTime * Input.GetAxis("Mouse X"));
|
||||
}
|
||||
|
||||
//if input is received from right analog stick (horizontal)
|
||||
else if(Input.GetAxis("HorizontalCam") != 0){
|
||||
transform.parent.Rotate(Vector3.up * sensitivity * Time.deltaTime * Input.GetAxis("HorizontalCam"));
|
||||
}
|
||||
|
||||
//if input is if input is received from Mouse Y
|
||||
if (Input.GetAxis("Mouse Y") != 0)
|
||||
{
|
||||
camRotation.x -= Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime;
|
||||
camRotation.x = Mathf.Clamp(camRotation.x, minAngle, maxAngle);
|
||||
cam.transform.localEulerAngles = camRotation;
|
||||
}
|
||||
|
||||
//if input is received from right analog stick (vertical)
|
||||
else if (Input.GetAxis("VerticalTurn") != 0){
|
||||
camRotation.x -= Input.GetAxis("VerticalTurn") * sensitivity * Time.deltaTime;
|
||||
camRotation.x = Mathf.Clamp(camRotation.x, minAngle, maxAngle);
|
||||
cam.transform.localEulerAngles = camRotation;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void GetHit(Vector3 dir, float force){
|
||||
//if (!IsLocalPlayer) { return; }
|
||||
dir.Normalize();
|
||||
dirHit = dir * force;
|
||||
SwitchState(RagdollState);
|
||||
}
|
||||
|
||||
public void CancelMomentum(){
|
||||
pStats.CurVel = 0;
|
||||
vel = Vector3.zero;
|
||||
moveX = Vector3.zero;
|
||||
moveZ = Vector3.zero;
|
||||
driftVel = Vector3.zero;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ca62a3355c93dd04a9c72b0327c603e2
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,59 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class MoveCrouchState : MoveBaseState
|
||||
{
|
||||
|
||||
//Slide Variables
|
||||
float originalTraction; // Traction before slide started
|
||||
RaycastHit slideRay; // slide raycast
|
||||
|
||||
public override void EnterState(MoveStateManager mSM){
|
||||
Debug.Log("Crouch State");
|
||||
|
||||
if(mSM.previousState != mSM.SlideState){
|
||||
//Initialize Important Stats On state enter
|
||||
mSM.pStats.CurVel = 0;
|
||||
originalTraction = mSM.pStats.Traction;
|
||||
mSM.gameObject.transform.eulerAngles = new Vector3(mSM.transform.localEulerAngles.x - 90, mSM.transform.localEulerAngles.y, mSM.transform.localEulerAngles.z);
|
||||
mSM.moveController.height *= .5f;
|
||||
mSM.pStats.Traction = 0.01f;
|
||||
}
|
||||
}
|
||||
|
||||
public override void UpdateState(MoveStateManager mSM){
|
||||
|
||||
}
|
||||
|
||||
public override void FixedUpdateState(MoveStateManager mSM){
|
||||
mSM.transform.Rotate(Vector3.forward * -mSM.sensitivity * Time.deltaTime * Input.GetAxis("Mouse X"));
|
||||
mSM.pStats.Traction += .004f;
|
||||
|
||||
///////ONCE WE HAVE IT SO SLIDE DOESNT ROTATE PLAYER MOVE THIS TO UPDATE
|
||||
//If player isn't pressing either Q or the joystick button they stop crouching if nothing is above them
|
||||
if((!Input.GetKey(KeyCode.JoystickButton1) && !Input.GetKey(KeyCode.Q))){
|
||||
if ((Physics.Raycast(mSM.gameObject.transform.position, mSM.slideUp, out slideRay, 5f) == false)){
|
||||
|
||||
ExitCrouchState(mSM);
|
||||
mSM.SwitchState(mSM.IdleState);
|
||||
}
|
||||
else{
|
||||
Debug.Log("Object above you");
|
||||
}
|
||||
}
|
||||
|
||||
mSM.SlideMovement();
|
||||
}
|
||||
|
||||
public override void OnCollisionEnter(MoveStateManager mSM){
|
||||
|
||||
}
|
||||
|
||||
public void ExitCrouchState(MoveStateManager mSM){
|
||||
mSM.gameObject.transform.localEulerAngles = new Vector3(0, 0, 0);
|
||||
mSM.pStats.CurVel = mSM.calculatedCurVel;
|
||||
mSM.pStats.Traction = originalTraction;
|
||||
mSM.moveController.height *= 2.0f;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 88c903fd0284d184dade30af31081373
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class MoveCrouchWalkState : MoveBaseState
|
||||
{
|
||||
public override void EnterState(MoveStateManager mSM){
|
||||
Debug.Log("Crouch Walk State");
|
||||
}
|
||||
|
||||
public override void UpdateState(MoveStateManager mSM){
|
||||
|
||||
}
|
||||
|
||||
public override void FixedUpdateState(MoveStateManager mSM){
|
||||
|
||||
}
|
||||
|
||||
public override void OnCollisionEnter(MoveStateManager mSM){
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 502d8c0b8175a794ab08d2798a29465c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,71 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class MoveSlideState : MoveBaseState
|
||||
{
|
||||
//Slide Variables
|
||||
float originalTraction; // Traction before slide started
|
||||
RaycastHit slideRay; // slide raycast
|
||||
|
||||
public override void EnterState(MoveStateManager mSM){
|
||||
Debug.Log("Slide State");
|
||||
|
||||
//Initialize Important Stats On state enter
|
||||
mSM.pStats.CurVel = 0;
|
||||
originalTraction = mSM.pStats.Traction;
|
||||
mSM.gameObject.transform.eulerAngles = new Vector3(mSM.transform.eulerAngles.x - 90, mSM.transform.eulerAngles.y, mSM.transform.eulerAngles.z);
|
||||
mSM.moveController.height *= .5f;
|
||||
mSM.pStats.Traction = 0.01f;
|
||||
}
|
||||
|
||||
public override void UpdateState(MoveStateManager mSM){
|
||||
|
||||
//if player comes to a stop while sliding they crouch
|
||||
if(mSM.calculatedCurVel < mSM.idleLimit){
|
||||
mSM.SwitchState(mSM.CrouchState);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override void FixedUpdateState(MoveStateManager mSM){
|
||||
mSM.transform.Rotate(Vector3.forward * -mSM.sensitivity * Time.deltaTime * Input.GetAxis("Mouse X"));
|
||||
mSM.pStats.Traction += .004f;
|
||||
|
||||
///////ONCE WE HAVE IT SO SLIDE DOESNT ROTATE PLAYER MOVE THIS TO UPDATE
|
||||
if((!Input.GetKey(KeyCode.JoystickButton1) && !Input.GetKey(KeyCode.Q))){
|
||||
if ((Physics.Raycast(mSM.gameObject.transform.position, mSM.slideUp, out slideRay, 5f) == false)){
|
||||
|
||||
//Determine which state to go into based on player speed
|
||||
if(mSM.calculatedCurVel < mSM.walkLimit){
|
||||
SlideToMoveState(mSM);
|
||||
mSM.SwitchState(mSM.WalkState);
|
||||
}
|
||||
else if(mSM.calculatedCurVel < mSM.runLimit){
|
||||
SlideToMoveState(mSM);
|
||||
mSM.SwitchState(mSM.JogState);
|
||||
}
|
||||
else{
|
||||
SlideToMoveState(mSM);
|
||||
mSM.SwitchState(mSM.RunState);
|
||||
}
|
||||
}
|
||||
else{
|
||||
Debug.Log("Object above you");
|
||||
}
|
||||
}
|
||||
|
||||
mSM.SlideMovement();
|
||||
}
|
||||
|
||||
public override void OnCollisionEnter(MoveStateManager mSM){
|
||||
|
||||
}
|
||||
|
||||
public void SlideToMoveState(MoveStateManager mSM){
|
||||
mSM.gameObject.transform.localEulerAngles = new Vector3(0, 0, 0);
|
||||
mSM.pStats.CurVel = mSM.calculatedCurVel;
|
||||
mSM.pStats.Traction = originalTraction;
|
||||
mSM.moveController.height *= 2.0f;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4338a9bf5309e75489870c3702150aa8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -5,15 +5,24 @@ using UnityEngine;
|
||||
public class MoveIdleState : MoveBaseState
|
||||
{
|
||||
public override void EnterState(MoveStateManager mSM){
|
||||
|
||||
Debug.Log("Idle State");
|
||||
}
|
||||
|
||||
public override void UpdateState(MoveStateManager mSM){
|
||||
|
||||
//Move to Walk State after speed increases
|
||||
if(mSM.calculatedCurVel >= mSM.idleLimit){
|
||||
mSM.SwitchState(mSM.WalkState);
|
||||
}
|
||||
|
||||
//If Q or joystick button1 crouch state
|
||||
if((Input.GetKey(KeyCode.JoystickButton1) || Input.GetKey(KeyCode.Q))){
|
||||
mSM.SwitchState(mSM.CrouchState);
|
||||
}
|
||||
}
|
||||
|
||||
public override void FixedUpdateState(MoveStateManager mSM){
|
||||
|
||||
mSM.DirectionalMovement();
|
||||
}
|
||||
|
||||
public override void OnCollisionEnter(MoveStateManager mSM){
|
||||
|
||||
@@ -5,15 +5,29 @@ using UnityEngine;
|
||||
public class MoveJogState : MoveBaseState
|
||||
{
|
||||
public override void EnterState(MoveStateManager mSM){
|
||||
|
||||
Debug.Log("Jog State");
|
||||
//Debug.Log(mSM.calculatedCurVel);
|
||||
}
|
||||
|
||||
public override void UpdateState(MoveStateManager mSM){
|
||||
|
||||
//move to run state if speed increases
|
||||
if(mSM.calculatedCurVel >= mSM.runLimit){
|
||||
mSM.SwitchState(mSM.RunState);
|
||||
}
|
||||
//move to walk if speed decreases
|
||||
else if(mSM.calculatedCurVel < mSM.walkLimit){
|
||||
mSM.SwitchState(mSM.WalkState);
|
||||
}
|
||||
|
||||
//move to slide if Q or JoystickButton1
|
||||
if((Input.GetKey(KeyCode.JoystickButton1) || Input.GetKey(KeyCode.Q))){
|
||||
mSM.SwitchState(mSM.SlideState);
|
||||
}
|
||||
}
|
||||
|
||||
public override void FixedUpdateState(MoveStateManager mSM){
|
||||
|
||||
mSM.DirectionalMovement();
|
||||
}
|
||||
|
||||
public override void OnCollisionEnter(MoveStateManager mSM){
|
||||
|
||||
@@ -5,15 +5,25 @@ using UnityEngine;
|
||||
public class MoveRunState : MoveBaseState
|
||||
{
|
||||
public override void EnterState(MoveStateManager mSM){
|
||||
|
||||
Debug.Log("Run State");
|
||||
//Debug.Log(mSM.calculatedCurVel);
|
||||
}
|
||||
|
||||
public override void UpdateState(MoveStateManager mSM){
|
||||
|
||||
//move to Jog if speed decreases
|
||||
if(mSM.calculatedCurVel < mSM.runLimit){
|
||||
mSM.SwitchState(mSM.JogState);
|
||||
}
|
||||
|
||||
//move to slide if Q or JoystickButton1
|
||||
if((Input.GetKey(KeyCode.JoystickButton1) || Input.GetKey(KeyCode.Q))){
|
||||
mSM.SwitchState(mSM.SlideState);
|
||||
}
|
||||
}
|
||||
|
||||
public override void FixedUpdateState(MoveStateManager mSM){
|
||||
|
||||
mSM.DirectionalMovement();
|
||||
}
|
||||
|
||||
public override void OnCollisionEnter(MoveStateManager mSM){
|
||||
|
||||
@@ -5,15 +5,29 @@ using UnityEngine;
|
||||
public class MoveWalkState : MoveBaseState
|
||||
{
|
||||
public override void EnterState(MoveStateManager mSM){
|
||||
|
||||
Debug.Log("Walk State");
|
||||
//Debug.Log(mSM.calculatedCurVel);
|
||||
}
|
||||
|
||||
public override void UpdateState(MoveStateManager mSM){
|
||||
|
||||
//move to Jog if speed increases
|
||||
if(mSM.calculatedCurVel >= mSM.jogLimit){
|
||||
mSM.SwitchState(mSM.JogState);
|
||||
}
|
||||
//move to Idle if speed decreases
|
||||
else if(mSM.calculatedCurVel < mSM.idleLimit){
|
||||
mSM.SwitchState(mSM.IdleState);
|
||||
}
|
||||
|
||||
//move to slide if Q or JoystickButton1
|
||||
if((Input.GetKey(KeyCode.JoystickButton1) || Input.GetKey(KeyCode.Q))){
|
||||
mSM.SwitchState(mSM.SlideState);
|
||||
}
|
||||
}
|
||||
|
||||
public override void FixedUpdateState(MoveStateManager mSM){
|
||||
|
||||
mSM.DirectionalMovement();
|
||||
}
|
||||
|
||||
public override void OnCollisionEnter(MoveStateManager mSM){
|
||||
|
||||
@@ -31,6 +31,7 @@ public class dPlayerMovement : NetworkBehaviour
|
||||
private Vector3 moveZ; // Local Horizontal Vector
|
||||
private Vector3 moveX; // Local Vertical Vector
|
||||
public Vector3 driftVel; // Lerped Movement Vector
|
||||
public float calculatedCurVel; // calculated Vel based on curVel
|
||||
|
||||
//Jump Variables
|
||||
public int curJumpNum; // current Jumps Used
|
||||
|
||||
Reference in New Issue
Block a user