- Home /
AddForce moving object in wrong direction
Hello everyone, I am having an issue on my "First person" world. I am currently hitting targets, but they are moving the wrong way as a result. For instance, if I swing forward and hit a target, it moves backwards (towards the screen). If I swing left, the object hit moves right. If I swing right the object moves left. If my avatar is in the middle, the objects hit always move towards the middle and hit the avatar.
Vector3 acceleration = (rigidbody.velocity - previousVelocity) / Time.fixedDeltaTime;
Vector3 force = rigidbody.mass * acceleration;
ResolveCollision(collisionObject, force);
void ResolveCollision(Collision col, Vector3 force)
{
col.collider.GetComponent<EnemyScript>().Hit(force);
}
public void Hit(Vector3 impulse)
{
rigidbody.AddForce(impulse, ForceMode.Impulse);
}
What happens if you invert 'impulse'? Isn't this just a simple arithmatic error?
Whats the best way to invert a vector? It is not all of the axis that are wrong, just whichever one the "fist" is traveling in. So if I punch forward, it is the z axis and if I punch left/right, it is the x. Though there can be combinations of the two as well.
Invert as in-
Vector3 invertedForce = -force;
it's literally that easy.
please make sure if your question is answered you check mark the check mark under the answer.
Answer by AchillesWF · Apr 23, 2012 at 02:35 AM
You need to define where you are updating previousVelocity (and rigidbody.velocity). My guess is that when you perform the attack, the object stops , gets an instantaneous velocity lower than previous, you get "negative" acceleration, and that leads to an opposite impulse.
Your answer
Follow this Question
Related Questions
Removing velocity relative to the contact normal 1 Answer
Unity3D: Get Velocity after collision 0 Answers
(Steam VR / Vive) Rigidbody moving in only one direction after collision 1 Answer
Why is the relativeVelocity of a Collision always zero in OnCollisionEnter and OnCollisionStay? 0 Answers
Resume movement of instanced object relative to the original after instantiation. 2 Answers