Reputation: 3
I am a fairly new coder and I was having some big issues with something and I wasn't able to find a nice fix just by searching it up. Essentially, I have followed the RigidBody-based movement tutorial linked here:
(https://www.youtube.com/watch?v=f473C43s8nE&t=1s).
In it, it creates the way that the player moves by using rb.AddForce, additionally, to stop the player from moving too fast and/or creating infinite velocity when airborne, it has a system that if the RigidBody's magnitude goes over the max speed, it will do something to stop the player from moving any faster. I am trying to implement a sort of "Boost/Dash" that would give the player a large amount of vertical and horizontal momentum, I currently do this through rb.AddForce. The problem is though, that I can't make the player move as fast as I want because of the Speed Control system, but if I remove the Speed Control the player will be able to go infinitely fast when in midair.
I have tried things such as adding the movement velocity to a holder RidgedBody and then limiting that before adding that to the player velocity. I have also tried stopping all wasd movement when traveling, it was able to allow for the boost to happen without any infinite velocity gain, but it also stopped all mid-air control which just felt really bad.
Basicly to sum up the entire problem, I need the player to not gain a ton of velocity simply by being airborne, although I need the player to still be capable of going faster than the basic movement speed cap, just they would only be capable of going that fast by forces other than simple movement like wasd and jumping.
private void Dash()
{
rb.velocity = new Vector3(0,0,0);
rb.angularVelocity = new Vector3(0,0,0);
rb.AddForce(orientation.forward * hDashForce, ForceMode.Impulse);
rb.AddForce(transform.up * vDashForce, ForceMode.Impulse);
}
private void MovePlayer()
{
moveDirection = orientation.forward * vInput + orientation.right * hInput;
if (grounded)
{
rb.AddForce(moveDirection.normalized * 10f * movespeed, ForceMode.Force);
}
else
{
rb.AddForce(moveDirection.normalized * 10f * movespeed * airMultiplier, ForceMode.Force);
}
}
private void SpeedControl()
{
Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
if (flatVel.magnitude > maxspeed)
{
Vector3 limitedVel = flatVel.normalized * maxspeed;
rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
}
}
Upvotes: 0
Views: 205
Reputation: 66
Could you use the state? (https://en.wikipedia.org/wiki/Finite-state_machine) I think dividing state between dash and normal move.
so... I change a code little different...
private void SpeedControl(){
Vector3 flatVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
float dashMaxSpeed = ??f;
if (state == dash)
maxspeed = dashMaxSpeed;
else
maxspeed = normalMaxSpeed;
if (flatVel.magnitude > maxspeed)
{
Vector3 limitedVel = flatVel.normalized * maxspeed;
rb.velocity = new Vector3(limitedVel.x, rb.velocity.y, limitedVel.z);
}
} like this... It is not good code but I expect can give you little idea.
Upvotes: 0