swing is mostly functional

Still needs some easing on movement and other minor changes
This commit is contained in:
Melbyj1125
2022-01-23 18:42:25 -06:00
parent 3306da25ba
commit f1e693e935
2 changed files with 90 additions and 32 deletions

View File

@@ -19,15 +19,26 @@ public class dGrapplingHook : NetworkBehaviour
[SerializeField] private float ropeLength;
private float climbRate = 5;
private Vector3 swingDirection;
private float swingSpeed = 90;
private float swingMom = 0;
float inclinationAngle;
float theta = -1;
private float maxSwingSpeed = 90;
private float minSwingSpeed = 10;
private float swingSpeed = 0;
private float swingMom = 40;
private Vector3 tensionMomDirection;
private Vector3 hookPointRight;
private Vector3 momDirection;
private Vector3 curXZDir;
private Vector3 oldXZDir;
private float flip = -1; // flip variables for swing momentum
private bool swingback = false; //swing the player back
private float midpointMom;
Vector3 tensionDirection;
Vector3 pendulumSideDirection;
Vector3 tangentDirection;
float tensionForce;
public Vector3 forceDirection;
private bool unhooking = false;
// Start is called before the first frame update
@@ -54,8 +65,9 @@ public class dGrapplingHook : NetworkBehaviour
{
hookPoint = hookPoints[hookPointIndex]; //The point we are grappling from
ropeLength = Vector3.Distance(gameObject.transform.position, hookPoint.transform.position) + 0.5f;
oldXZDir = (new Vector3(hookPoint.transform.position.x,0,hookPoint.transform.position.z) - new Vector3(transform.position.x,0,transform.position.z)).normalized;
curXZDir = (new Vector3(hookPoint.transform.position.x,0,hookPoint.transform.position.z) - new Vector3(transform.position.x,0,transform.position.z)).normalized;
isGrappled = true; //toggle grappling state
unhooking = false;
}
}
else //Else we are grappling
@@ -72,7 +84,8 @@ public class dGrapplingHook : NetworkBehaviour
{
Debug.DrawRay(gameObject.transform.position, (hookPoint.transform.position - gameObject.transform.position)); //Visual of line
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.JoystickButton4)) //Extend hook
//Extend Hook
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.JoystickButton4))
{
ropeLength += climbRate * Time.deltaTime;
if (ropeLength > maxGrappleDistance)
@@ -81,7 +94,9 @@ public class dGrapplingHook : NetworkBehaviour
}
//Debug.Log(ropeLength.ToString());
}
if (Input.GetKey(KeyCode.RightShift) || Input.GetKey(KeyCode.JoystickButton5)) // Retract Hook
//Retract Hook
if (Input.GetKey(KeyCode.RightShift) || Input.GetKey(KeyCode.JoystickButton5))
{
ropeLength -= climbRate * Time.deltaTime;
if (ropeLength < 5)
@@ -90,24 +105,40 @@ public class dGrapplingHook : NetworkBehaviour
}
//Debug.Log(ropeLength.ToString());
}
//Do grappling physics based on hookPoint
if (Vector3.Distance(gameObject.transform.position, hookPoint.transform.position) > ropeLength )
//Debug.Log(Vector3.Distance(gameObject.transform.position, hookPoint.transform.position));
Debug.Log(ropeLength);
//Calculate tether force direction based on hookpoint
if (Vector3.Distance(gameObject.transform.position, hookPoint.transform.position) >= ropeLength )
{
forceDirection = calculateForceDirection(1, playerMovement.g, hookPoint.transform.position);
forceDirection = calculateForceDirection(1, playerMovement.g+.01f, hookPoint.transform.position);
}
movementController.Move(swingMoveController());
//apply special swing movement when aerial
if(!playerMovement.isGrounded){
movementController.Move(swingMoveController());
if(swingMom != 0){
movementController.Move(calculateMomentumDirection(playerMovement.g, hookPoint.transform.position));
swingMom -= .5f;
}
}
if(swingMom<0) swingMom = 0;
}
else if(!isGrappled && !unhooking){
this.gameObject.transform.localEulerAngles = new Vector3(0, 0, 0);
unhooking = true;
else if(!isGrappled || playerMovement.isGrounded){
flip = -1;
swingback = true;
swingMom = 50;
}
//WILL NEED ADJUSTMENT OR REMOVAL IN THE FUTURE
//ungrapple on jump
if(playerMovement.GetJumpPressed()) isGrappled = false;
//Reset force direction after unhook
if(isGrappled == false) forceDirection = Vector3.zero;
}
//Finds the nearest hook to the player
int FindHookPoint()
{
float least = maxGrappleDistance;
@@ -123,37 +154,65 @@ public class dGrapplingHook : NetworkBehaviour
return index;
}
//Calculate the tether direction vector and how much force that vector needs
Vector3 calculateForceDirection(float mass, float g, Vector3 hPoint){
tensionDirection = (hPoint - transform.position).normalized;
float inclinationAngle = Vector3.Angle((transform.position - hPoint).normalized, -transform.up);
float theta = Mathf.Deg2Rad * inclinationAngle;
if(theta <= .3 && swingMom <= 0 && theta > 0){
theta -= .05f;
}
if(theta<0) theta = 0;
Debug.Log(theta);
//tension direction and angle calculation
tensionDirection = (hPoint - transform.position).normalized;
inclinationAngle = Vector3.Angle((transform.position - hPoint).normalized, -transform.up);
theta = Mathf.Deg2Rad * inclinationAngle;
if(theta<=.1) theta = 0;
//How much force the tension needs
tensionForce = mass * -g * Mathf.Cos(theta);
//force direction calculation based on tension direction and force
Vector3 fDirection = tensionDirection * tensionForce;
return fDirection;
}
Vector3 calculateMomentumDirection(float g, Vector3 hPoint){
tensionMomDirection = (hPoint - transform.position).normalized;
hookPointRight = Vector3.Cross((new Vector3(hPoint.x,0,hPoint.z) - new Vector3(transform.position.x,0,transform.position.z)), transform.up).normalized;
momDirection = flip * Vector3.Cross(hookPointRight, tensionMomDirection).normalized;
if(oldXZDir != curXZDir && flip == -1){
flip = 1;
midpointMom = swingMom;
swingback = false;
Debug.Log("flip");
}
if(swingback == false && swingMom <= (midpointMom*(.75f))){
swingback = true;
flip = -1;
oldXZDir = (new Vector3(hookPoint.transform.position.x,0,hookPoint.transform.position.z) - new Vector3(transform.position.x,0,transform.position.z)).normalized;
Debug.Log("swingback");
}
curXZDir = (new Vector3(hPoint.x,0,hPoint.z) - new Vector3(transform.position.x,0,transform.position.z)).normalized;
Debug.DrawRay(transform.position, momDirection * Time.deltaTime* 100, Color.green);
return (momDirection * Time.deltaTime * swingMom);
}
//Special movement for the player while they swing
Vector3 swingMoveController(){
//WASD input
float inputVert = Input.GetAxis("Vertical");
float inputHor = Input.GetAxis("Horizontal");
//input is zero when nothing is pressed to prevent button easing values
if((!Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.S))) inputVert = 0;
if((!Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D))) inputHor = 0;
//Movement Vector for the player to move in
//Swing direction based on player input
swingDirection = Vector3.Cross(tensionDirection, ((transform.right * -inputVert) + (transform.forward * inputHor))).normalized;
//Actual swing movement vector with speed applied
Vector3 swingMovement = (swingDirection * Time.deltaTime * swingSpeed);
//NEED TO ADD SWINGSPEED EASING
//Swing movement with swing speed added
Vector3 swingMovement = (swingDirection * Time.deltaTime * maxSwingSpeed);
return (swingMovement);
}

View File

@@ -194,7 +194,6 @@ public class dPlayerMovement : NetworkBehaviour
vel = moveX + moveZ;
Vector3 moveXZ = new Vector3(vel.x, 0, vel.z);
driftVel = Vector3.Lerp(driftVel, moveXZ, pStats.Traction * Time.deltaTime);
if(grapple.isGrappled) driftVel = Vector3.zero;
//Gravity and Jump calculations
UpdateGravity();
@@ -221,7 +220,7 @@ public class dPlayerMovement : NetworkBehaviour
}
//Move Player
if(grapple.isGrappled && !isGrounded){
moveController.Move(driftVel + ((moveY + grapple.forceDirection) * Time.deltaTime));
moveController.Move(((moveY + grapple.forceDirection) * Time.deltaTime));
}
else{
moveController.Move(driftVel + (moveY * Time.deltaTime));
@@ -237,7 +236,7 @@ public class dPlayerMovement : NetworkBehaviour
{
WallCheck();
//If nothing is pressed speed is 0
if ((Input.GetAxis("Vertical") == 0.0f && Input.GetAxis("Horizontal") == 0.0f) || isSliding)
if ((Input.GetAxis("Vertical") == 0.0f && Input.GetAxis("Horizontal") == 0.0f) || isSliding ||(grapple.isGrappled && !isGrounded))
{
pStats.CurVel = 0.0f;
return pStats.CurVel;