mirror of
https://github.com/Leahnaya/TheKingsRace.git
synced 2026-08-21 18:04:27 -05:00
Did some bugfixing and commented on everything
This commit is contained in:
@@ -4,21 +4,24 @@ using UnityEngine;
|
||||
|
||||
public class AerialGrappleAirState : AerialBaseState
|
||||
{
|
||||
|
||||
bool spaceHeld = true;
|
||||
float ropeLength; // current rope length
|
||||
Vector3 swingDirection; // Swing direction
|
||||
float inclinationAngle; // inclination angle
|
||||
float theta = -1; // theta
|
||||
float swingMom;
|
||||
Vector3 tensionMomDirection;
|
||||
Vector3 hookPointRight;
|
||||
Vector3 momDirection;
|
||||
Vector3 curXZDir;
|
||||
Vector3 oldXZDir;
|
||||
float swingSpeed = 10;
|
||||
bool swingback = false; //swing the player back
|
||||
float oldSwingMom;
|
||||
Vector3 tensionDirection;
|
||||
float tensionForce;
|
||||
float theta = -1; // theta for rope angle
|
||||
Vector3 hookPointRight; // right vector of the hook point
|
||||
Vector3 curXZDir; // current straight line between player and hook point ignoring y axis
|
||||
Vector3 oldXZDir; // old straight line between player and hook point ignoring y axis
|
||||
|
||||
Vector3 swingDirection; // Swing direction
|
||||
float swingSpeed = 10; // cur swing speed
|
||||
Vector3 tensionDirection; // tension direction
|
||||
float tensionForce; // tension force amplifier
|
||||
|
||||
float swingMom; // swing Mom amplifier
|
||||
float oldSwingMom; // old swing Momentum amplifier
|
||||
Vector3 momDirection; // momentum direction
|
||||
Vector3 tensionMomDirection; // tension momentum direction
|
||||
|
||||
public override void EnterState(AerialStateManager aSM, AerialBaseState previousState){
|
||||
|
||||
@@ -43,48 +46,61 @@ public class AerialGrappleAirState : AerialBaseState
|
||||
aSM.pStats.GravVel = -1; // grav vel is adjusted so things work
|
||||
aSM.release = false; // player hasn't released
|
||||
aSM.lerpRelease = Vector3.zero; // reset lerp release
|
||||
spaceHeld = true;
|
||||
}
|
||||
|
||||
public override void ExitState(AerialStateManager aSM, AerialBaseState nextState){
|
||||
|
||||
//If not going into grapple grounded state
|
||||
if(nextState != aSM.GrappleGroundedState){
|
||||
aSM.release = true;
|
||||
aSM.pStats.GravVel = 0;
|
||||
aSM.forceDirection = Vector3.zero;
|
||||
swingback = true;
|
||||
}
|
||||
|
||||
//If going into grapple grounded state
|
||||
else{
|
||||
aSM.pStats.GravVel = 0;
|
||||
aSM.forceDirection = Vector3.zero;
|
||||
swingback = true;
|
||||
}
|
||||
}
|
||||
|
||||
public override void UpdateState(AerialStateManager aSM){
|
||||
|
||||
if((Input.GetKeyDown(KeyCode.E) || Input.GetKeyDown(KeyCode.JoystickButton2)) && !aSM.eHeld){
|
||||
|
||||
//if pressing E or ragdolling then falling
|
||||
if(((Input.GetKeyDown(KeyCode.E) || Input.GetKeyDown(KeyCode.JoystickButton2)) && !aSM.eHeld) || (aSM.mSM.currentState == aSM.mSM.RagdollState)){
|
||||
aSM.SwitchState(aSM.FallingState);
|
||||
}
|
||||
else if((Input.GetKeyUp(KeyCode.E) || Input.GetKeyUp(KeyCode.JoystickButton2))){
|
||||
else if((Input.GetKeyUp(KeyCode.E) || Input.GetKeyUp(KeyCode.JoystickButton2)) && aSM.eHeld){
|
||||
aSM.eHeld = false;
|
||||
}
|
||||
|
||||
if(Input.GetButton("Jump")){
|
||||
|
||||
//if pressing Jump then jump
|
||||
else if(Input.GetButton("Jump") && !spaceHeld){
|
||||
aSM.SwitchState(aSM.JumpingState);
|
||||
}
|
||||
else if(!(Input.GetButton("Jump")) && spaceHeld){
|
||||
spaceHeld = false;
|
||||
}
|
||||
|
||||
if(aSM.isGrounded){
|
||||
//if grounded then grapple grounded
|
||||
else if(aSM.isGrounded){
|
||||
aSM.SwitchState(aSM.GrappleGroundedState);
|
||||
}
|
||||
|
||||
//if wallrunning then wallrun
|
||||
else if(aSM.isWallRunning){
|
||||
aSM.SwitchState(aSM.WallRunState);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override void FixedUpdateState(AerialStateManager aSM){
|
||||
|
||||
|
||||
////////ADD A LINE RENDERER WHEN WE GET THE HAND MODEL
|
||||
//Draw Line between player and hookpoint for debug purposes
|
||||
Debug.DrawRay(aSM.transform.position, (aSM.hookPoint.transform.position - aSM.transform.position)); //Visual of line
|
||||
|
||||
//Debug.Log(Vector3.Distance(gameObject.transform.position, hookPoint.transform.position));
|
||||
//Calculate tether force direction based on hookpoint
|
||||
if (Vector3.Distance(aSM.transform.position, aSM.hookPoint.transform.position) >= ropeLength )
|
||||
{
|
||||
@@ -94,26 +110,23 @@ public class AerialGrappleAirState : AerialBaseState
|
||||
aSM.forceDirection = Vector3.zero;
|
||||
}
|
||||
|
||||
//Move player based on their inputs
|
||||
aSM.moveController.Move(SwingMoveController(aSM));
|
||||
|
||||
//if Swing Momentum isn't zero then move player
|
||||
if(swingMom != 0){
|
||||
aSM.moveController.Move(CalculateMomentumDirection(aSM.pStats.GravVel, aSM.hookPoint.transform.position, aSM));
|
||||
swingMom -= .5f;
|
||||
}
|
||||
|
||||
if(swingMom<0) swingMom = 0;
|
||||
|
||||
//Calculate temp release at every position
|
||||
aSM.tempRelease = CalculateSwingReleaseForce();
|
||||
aSM.GravityCalculation(aSM.pStats.PlayerGrav);
|
||||
}
|
||||
|
||||
//Needs to be Overhauled to properly implement energy loss taking into account players current Velocity, height and input
|
||||
//------Important Equations--------
|
||||
// TensionForce = Cos(theta) * g * m * Vector3(tensionDirection)
|
||||
// Potential Energy = m * g * h -- Will need to modify adding current speed
|
||||
// Kinetic Energy = 1/2 * m * V^2 -- assuming no energy loss at the bottom KE = PE at peak we will add energy loss ourselves
|
||||
// Total Energy = m * ((g*h) + (1/2 * V^2))
|
||||
// Velocity = (2 * ((TotalEnergy/m) - (g*h)))^(1/2) -- This hypothetically could be used for our swing momentum calculation
|
||||
// Movement Direction right angle of tension force = Sin(theta) * g * m * Vector3(Right angle to tensionDirection)
|
||||
//Apply default gravity
|
||||
aSM.GravityCalculation(aSM.pStats.PlayerGrav);
|
||||
|
||||
}
|
||||
|
||||
//Calculate the tether direction vector and how much force that vector needs
|
||||
Vector3 CalculateForceDirection(float mass, float g, Vector3 hPoint, AerialStateManager aSM){
|
||||
@@ -129,7 +142,10 @@ public class AerialGrappleAirState : AerialBaseState
|
||||
|
||||
//force direction calculation based on tension direction and force
|
||||
Vector3 fDirection = tensionDirection * tensionForce;
|
||||
|
||||
//return force direction
|
||||
return fDirection;
|
||||
|
||||
}
|
||||
|
||||
Vector3 CalculateMomentumDirection(float g, Vector3 hPoint, AerialStateManager aSM){
|
||||
@@ -137,23 +153,20 @@ public class AerialGrappleAirState : AerialBaseState
|
||||
tensionMomDirection = (hPoint - aSM.transform.position).normalized;
|
||||
hookPointRight = Vector3.Cross(oldXZDir, aSM.transform.up).normalized;
|
||||
momDirection = -1 * Vector3.Cross(hookPointRight, tensionMomDirection).normalized;
|
||||
|
||||
if(oldXZDir != curXZDir){
|
||||
//midpointMom = swingMom;
|
||||
swingback = false;
|
||||
//Debug.Log("flip");
|
||||
}
|
||||
|
||||
if(swingback == false && swingMom <= (oldSwingMom*(.75f))){
|
||||
swingback = true;
|
||||
//if player is on the other side of hookpoint and swingMom is lower then update oldXZDir
|
||||
if(oldXZDir != curXZDir && swingMom <= (oldSwingMom*(.75f))){
|
||||
oldSwingMom = swingMom;
|
||||
oldXZDir = (new Vector3(aSM.hookPoint.transform.position.x,0,aSM.hookPoint.transform.position.z) - new Vector3(aSM.transform.position.x,0,aSM.transform.position.z)).normalized;
|
||||
//Debug.Log("swingback");
|
||||
}
|
||||
|
||||
//current line between hookpoint and player
|
||||
curXZDir = (new Vector3(hPoint.x,0,hPoint.z) - new Vector3(aSM.transform.position.x,0,aSM.transform.position.z)).normalized;
|
||||
Debug.DrawRay(aSM.transform.position, momDirection * Time.deltaTime* 100, Color.green);
|
||||
|
||||
//return momentum dir * mom force
|
||||
return (momDirection * Time.deltaTime * swingMom);
|
||||
|
||||
}
|
||||
|
||||
//Calculates the players initial swing momentum using their height and their current velocity
|
||||
@@ -165,11 +178,15 @@ public class AerialGrappleAirState : AerialBaseState
|
||||
swingHeight = 1;
|
||||
}
|
||||
|
||||
float sMom = playerSpeed + (swingHeight*2);
|
||||
//calculates swing momentum based on height ands speed
|
||||
float sMom = playerSpeed + (swingHeight * 2.5f);
|
||||
if(sMom > aSM.maxSwingMom){
|
||||
sMom = aSM.maxSwingMom;
|
||||
}
|
||||
|
||||
//returns swing momentum
|
||||
return sMom;
|
||||
|
||||
}
|
||||
|
||||
//Special movement for the player while they swing
|
||||
@@ -197,14 +214,12 @@ public class AerialGrappleAirState : AerialBaseState
|
||||
//Swing direction based on player input
|
||||
swingDirection = Vector3.Cross(tensionDirection, ((aSM.transform.right * -inputVert) + (aSM.transform.forward * inputHor))).normalized;
|
||||
|
||||
|
||||
//NEED TO ADD SWINGSPEED EASING
|
||||
//this could be just adding a gradual increase in the swing speed instead of using a flat rate
|
||||
//Swing movement with swing speed added
|
||||
Vector3 swingMovement = (swingDirection * Time.deltaTime * swingSpeed);
|
||||
|
||||
|
||||
//returns swing movement vector
|
||||
return (swingMovement);
|
||||
|
||||
}
|
||||
|
||||
Vector3 RopeLengthOffset(Vector3 hPoint, float curDistance, AerialStateManager aSM){
|
||||
@@ -215,19 +230,24 @@ public class AerialGrappleAirState : AerialBaseState
|
||||
//The direction we need to apply force to offset when the rope gets lengthened beyond the necessary point
|
||||
Vector3 tenDirOffset = (hPoint - aSM.transform.position).normalized;
|
||||
|
||||
|
||||
//returns rope offset direction * power
|
||||
return tenDirOffset * offsetPower * Time.deltaTime;
|
||||
|
||||
}
|
||||
|
||||
Vector3 CalculateSwingReleaseForce(){
|
||||
|
||||
|
||||
//Swing release direction
|
||||
Vector3 releaseSwingForceDirection = momDirection * ((swingMom) + 10);
|
||||
|
||||
releaseSwingForceDirection = new Vector3(releaseSwingForceDirection.x,0,releaseSwingForceDirection.z);
|
||||
|
||||
if(swingMom < 5){
|
||||
//if swingMom is low there is no release force
|
||||
if(swingMom < 2){
|
||||
return Vector3.zero;
|
||||
}
|
||||
|
||||
//return swing release direction
|
||||
return releaseSwingForceDirection * Time.deltaTime;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,8 @@ public class AerialGrappleGroundedState : AerialBaseState
|
||||
{
|
||||
public override void EnterState(AerialStateManager aSM, AerialBaseState previousState){
|
||||
|
||||
aSM.release = false;
|
||||
aSM.release = false; // release is false when grounded
|
||||
|
||||
}
|
||||
|
||||
public override void ExitState(AerialStateManager aSM, AerialBaseState nextState){
|
||||
@@ -14,25 +15,32 @@ public class AerialGrappleGroundedState : AerialBaseState
|
||||
}
|
||||
|
||||
public override void UpdateState(AerialStateManager aSM){
|
||||
if(!aSM.isGrounded && aSM.pStats.GravVel < 0){
|
||||
|
||||
//if E is pressed or ragdolling then grounded
|
||||
if(((Input.GetKeyDown(KeyCode.E) || Input.GetKeyDown(KeyCode.JoystickButton2)) && !aSM.eHeld) || (aSM.mSM.currentState == aSM.mSM.RagdollState)){
|
||||
aSM.SwitchState(aSM.GroundedState);
|
||||
}
|
||||
else if((Input.GetKeyUp(KeyCode.E) || Input.GetKeyUp(KeyCode.JoystickButton2)) && aSM.eHeld){
|
||||
aSM.eHeld = false;
|
||||
}
|
||||
|
||||
//if not grounded and gravVel < 0 then grapple air
|
||||
else if(!aSM.isGrounded && aSM.pStats.GravVel < 0){
|
||||
aSM.SwitchState(aSM.GrappleAirState);
|
||||
}
|
||||
|
||||
if(Vector3.Distance(aSM.transform.position, aSM.hookPoint.transform.position) > aSM.maxGrappleDistance){
|
||||
//if distance between player and hookpoint is too far then grounded
|
||||
else if(Vector3.Distance(aSM.transform.position, aSM.hookPoint.transform.position) > aSM.maxGrappleDistance){
|
||||
aSM.SwitchState(aSM.GroundedState);
|
||||
}
|
||||
|
||||
if((Input.GetKeyDown(KeyCode.E) || Input.GetKeyDown(KeyCode.JoystickButton2)) && !aSM.eHeld){
|
||||
aSM.SwitchState(aSM.GroundedState);
|
||||
}
|
||||
else if((Input.GetKeyUp(KeyCode.E) || Input.GetKeyUp(KeyCode.JoystickButton2))){
|
||||
aSM.eHeld = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override void FixedUpdateState(AerialStateManager aSM){
|
||||
Debug.DrawRay(aSM.transform.position, (aSM.hookPoint.transform.position - aSM.transform.position)); //Visual of line
|
||||
|
||||
//Default gravity calculation
|
||||
aSM.GravityCalculation(aSM.pStats.PlayerGrav);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user