I’m trying to create a boost in speed and then the speed goes back to its original value using lerp, but the way I’m modifying my speed is somehow causing me to lose control of my character for a second once the speed starts to ramp back down. Could someone take a look at my code and tell me what I’m doing wrong?
By Lose control of my speed I mean whichever way I’m moving at that point where my speed starts to go back down, I cant change directions for at least a second.(its like im on ice at that point)
these are the only two functions that deal with my speed and my Checks function which right now only checks to see if im grounded
void Checks () // Standard checks
{
// START GROUND CHECK
if (Physics.Raycast(transform.position, -transform.up, out normalHit, playerToNormalRay, groundMask)) // Checks to see if the player is grounded.
{
isGrounded = true;
}
else
{
isGrounded = false;
}
// END GROUND CHECK
}
This function is just for basic input and movement.
void Movement() // Players base movement
{
// Debug.Log(_moveDirection);
_moveDirection = new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed, Input.GetAxis("Jump") * jumpForce, Input.GetAxisRaw("Vertical") * moveSpeed); // Vector3 for movement input.
if (isGrounded) // Stops the player from moving while airborne
{
if (rb.velocity.magnitude < curSpeed /* moveSpeed */) // Stops the speed from multiplying out into neverland
{
rb.AddRelativeForce(_moveDirection); // Using AddRelativeForce to move based on the players forward
}
}
}
moveSpeed is a float set to 15
_moveDirection is a Vector3
rb is the Rigidbody.
Below is where I think my problem lies (in the lerps)
This function handles the speed boost with lerps to ramp the speed up and then back down.
The part that handles the speed ramps starts with the if statement with the speedChange bool in the () brackets.
void PlayerMomemtum() // Players Speed Momemtum
{
playersYVelocity = rb.velocity.y; // Players Y velocity at all times.
if (isGrounded && playersYVelocity < 0 && velocitySwitch) // Runs once when the player is grounded, falling, and when the check velocitySwitch is reset.
{
playersYContactVelocity = playersYVelocity; // Grabs the last instance of playersYVelocity before hitting the ground.
// rb.drag = playersLiftOffDrag;
speedChange = true;
velocitySwitch = false;
}
else if (playersYVelocity > 0) // Resets velocityGrab when the Player is going upwards in world space.
{
velocitySwitch = true;
}
if (speedChange)
{
curSpeed = Mathf.Lerp(curSpeed, maxSpeed, Time.deltaTime);
if (curSpeed > maxSpeed - 1)
{
speedChange = false;
}
}
else if (speedChange == false)
{
curSpeed = Mathf.Lerp(curSpeed, minSpeed, Time.deltaTime);
}
}
playerYVelocity is a float.
curSpeed is the current speed
minSpeed is set to 15
maxSpeed is set to 25
All are floats
All functions are in Update
Let me know if I need to include more, any help is greatly appreciated.