- Home /
Why AddForce up is higher than AddForce right?
Hi, i want to hit enemy and move the enemy up or right depending on animation and using same force speed,
but when i do that addforce up launches far in sky not like addforce right using same force speed.
float forceSpeed=100;
bool up=false; bool right=false;
// in update
if (Input.GetKeyDown(KeyCode.R)) up = true;
else if (Input.GetKeyDown(KeyCode.F)) right = true;
// in fixed update
if (up)
{
GetComponent<Rigidbody>().AddForce(Vector3.up * forceSpeed);
up = false;
}
else if (right)
{
GetComponent<Rigidbody>().AddForce(Vector3.right * forceSpeed);
right = false;
}
not same outcome, different magnitude.
mass=1, drag=0, angdrag=0.05,
use gravity=true,
not using physicsMaterial on gound.
Answer by Eno-Khaon · Nov 06, 2020 at 07:27 AM
When a Rigidbody is touching a Collider, friction will slow any remaining motion in the Rigidbody.
When you launch that Rigidbody upward, it'll (presumably) no longer be touching any surface, so only aerial drag (which you mention is set to 0) will slow it down, rather than both drag and friction from contact.
One option to force the object harder to the side would be to apply a small amount of upward force as well, so the object is knocked back and slightly upward instead.
As an additional note, since you're using single-frame impulses (assuming you're using default physics/Time.fixedDeltaTime rate), you can divide the impulse force by 50 and apply it as a VelocityChange instead (to also ignore mass).
float forceSpeed=2; // 100 / 50
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
// ...
rb.AddForce(Vector3.up * forceSpeed, ForceMode.VelocityChange);
// ...
// Simple example of adding a slight upward force to the horizontal impulse
rb.AddForce(Vector3.right * forceSpeed + Vector3.up * forceSpeed * 0.25f, ForceMode.VelocityChange);
Your answer
Follow this Question
Related Questions
Get result (force & torque) of AddForceAtPosition? 2 Answers
Why is force only being added in the same direction? 1 Answer
How do I add force To rigidbody based On Character's Walking direction? 0 Answers
Erratic Physics behaviour while testing in 2 computers 0 Answers
Prevent Rigidbody From Rotating 3 Answers