- Home /
Limiting player addforce speed excluding when hit by explosion
Hey guys I'm pretty new to this.. I am currently creating a 3d (2d movement) game where you must shoot other players off of a platform. The problem i have encountered is through limiting the player speed in the fixedupdate, the player does not get influenced by the explosion as much as i would like. I want to find a way to limit the player addforce speed without accelerating like crazy and without hindering how much the player is influenced by the explosion. my code is below thanks in advance :)
public void Move(Vector3 moveDirection, bool jump)
{
// Otherwise add force in the move direction.
{
m_Rigidbody.AddForce(moveDirection * m_MovePower);
}
// If on the ground and jump is pressed...
if (Physics.Raycast(transform.position, -Vector3.up, k_GroundRayLength) && jump)
{
// ... add force in upwards.
m_Rigidbody.AddForce(Vector3.up * m_JumpPower, ForceMode.Impulse);
}
}
private void FixedUpdate()
{
//both of these limit our left right max movement speed while still being able to fall exponentially faster due to gravity
if (m_Rigidbody.velocity.x > maxSpeed)
{
m_Rigidbody.velocity = new Vector2(maxSpeed, m_Rigidbody.velocity.y);
}
if (m_Rigidbody.velocity.x < -maxSpeed)
{
m_Rigidbody.velocity = new Vector2(-maxSpeed, m_Rigidbody.velocity.y);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Damage")
{
Vector3 force = transform.position - other.transform.position;
force.Normalize();
m_Rigidbody.AddForce(force * BlastTotal);
if (Weakness < 20)
{
Weakness += 1;
transform.localScale += new Vector3(0.05F, 0.05f, 0.05f);
}
}
}
Answer by Trissykins · Aug 28, 2019 at 05:14 AM
So I ended up figuring out a way to fix the issue :) thanks @Bunny83 for giving me the idea!
Basically what i did was separate the left and right movement so that unity can tell which way we are intending to go. I did this using
if (Input.GetAxis("Horizontal") > 0)
{
if (m_Rigidbody.velocity.x <= maxSpeed)
{
m_Rigidbody.AddForce(Vector3.right * m_MovePower);
}
else return;
}
if (Input.GetAxis("Horizontal") < 0)
{
if (m_Rigidbody.velocity.x >= -maxSpeed)
{
m_Rigidbody.AddForce(-Vector3.right * m_MovePower);
}
else return;
}
by doing this if a players intended direction is left, but they are already travelling fast in that direction, no addforce will be applied. (This works the same for the right). This means that the player is still able to be affected by explosives and travel at a much greater speed than the movement system would allow the player to reach on their own!
Answer by Cornelis-de-Jager · Aug 19, 2019 at 05:43 AM
Try this simple solution:
void FixedUpdate () {
// Split the direction and magnitude
var dir = m_Rigidbody.normalized;
var mag = m_Rigidbody.magnitude;
// Check if we are exeding the max speed
if (mag > maxSpeed)
m_Rigidbody.Veclocity = maxSpeed * dir;
}
I'm not having any issues with limiting the speed. That part of the code works almost too well in this case. The problem is I'm trying to limit the speed a player can get to from their own input, while still having the player able to travel much faster when hit with an explosion. I don't know how to limit one and not the other. I'm making a game similar to brackeys "ball wars" game if that helps add to the context :)
Answer by Bunny83 · Aug 21, 2019 at 10:08 AM
The only two solutions for such cases are:
stop adding forces in a direction if the velocity is already larger than the wanted max speed.
limiting the velocity but add a special case when applying explosion forces you add a bool to temporarily disable the limiting code.
Both cases have some flaws. Disable AddForce when you reached the wanted velocity can allow the player to slightly overshoot the wanted velocity. The second case might cause some glitches since you have to re-enable the velocity limiting code at some point. This can be either after a certain time or when a certain event happens (like player hits the floor or something).
I'm currently not home so I can't write any example code at the moment.