- Home /
Push Object in Opposite Direction of Collision
I want my player character gameObject to be pushed a short distance away from an enemy it comes in contact with. So I want to use OnCollisionEnter, if colliding object has the tag enemy, for the gameObject to move a short distance in the opposite direction of the enemy object it collided with. How would I go about doing this?
I think you should give it a shot and show the code that you use if it doesn't work out, but this sounds like you just want someone to supply you with the code.
Anyways, in the OnCollisionEnter function you need to find out where the enemy is in relation to the player object. So, if it is an object which is tagged as enemy, subtract the player position by the enemy position to find the direction to move in and then use that direction vector to move your player in that direction by whatever means you are using for movement.
Answer by MythralFTW · Nov 19, 2015 at 09:12 PM
This should accomplish your needs, let me know if you have questions.
void OnCollisionEnter(Collision c)
{
// force is how forcefully we will push the player away from the enemy.
float force = 3;
// If the object we hit is the enemy
if (c.gameObject.tag == "enemy")
{
// Calculate Angle Between the collision point and the player
Vector3 dir = c.contacts[0].point - transform.position;
// We then get the opposite (-Vector3) and normalize it
dir = -dir.normalized;
// And finally we add force in the direction of dir and multiply it by force.
// This will push back the player
GetComponent<Rigidbody>().AddForce(dir*force);
}
}
Thank you, this worked, but (and I say just in case someone else looks to this page for the same information) I had to make the force value much greater than 3 to see any noticeable effect.
I have a quick question. Assu$$anonymous$$g I want to use this code in a 2D environment - i changed it to "OnCollisionEnter2D" and changed the vector3 in line 10 to vector2 - but I'm still getting the error that "contacts" (line 10 as well) is not defined...any idea what i have to change in order to make it work? Thx for any help :)
did you put, OnCollisionEnter2D( Collision2D c )?
Couldn't get it to work - i'm now using:
void OnTriggerEnter2D (Collider2D other)
{
wallTrigger = false;
}
void OnTriggerExit2D (Collider2D other)
{
wallTrigger = true;
var magnitude = 2500;
var force = transform.position - other.transform.position;
force.Normalize ();
GetComponent<Rigidbody2D> ().AddForce (-force * magnitude);
}
Seems a little rough, but works fine for now ;) Thx for the help though!
Thank you! Never would've figured this one out on my own
Answer by kingcoyote · Nov 19, 2015 at 08:11 PM
Assuming you are using rigidbodies with correctly set mass and physics interaction layers...
Inside the OnCollisionEnter function, add this code:
public void OnCollisionEnter(Collision other) {
// how much the character should be knocked back
var magnitude = 5000;
// calculate force vector
var force = transform.position - other.transform.position;
// normalize force vector to get direction only and trim magnitude
force.Normalize();
gameObject.GetComponent<RigidBody>().AddForce(force * magnitude);
}
you have to add a physics material 2d with bouncing in 1, if used in 2d
This worked a lot better than the most liked answer for me. With the other answer's solution my collisions weren't always detecting for me for some reason. Thanks ;)
Answer by normoustgstaff · Feb 06, 2019 at 01:33 AM
Hi, I want to make the same think but with kimenatic object, and its not working I have tried to make it with velocity but the object starts to move constantly.
Any suggestions ?,Hi , I want to do the same but with kinematic object, and its not working , i have tried with velocity but the object start to move constantly.
Any suggestions ?
You will have to use $$anonymous$$athf.Lerp to start with very low velocity and increase it every frame until you reach desired velocity. This will simulate acceleration.
Your answer
Follow this Question
Related Questions
Rigidbody character controller can't walk on stairs 0 Answers
Rigidbody.SweepTest Hit Trigger Colliders 0 Answers
What is the best way to prevent a physics object from going through thin colliders? 2 Answers
Issue with adding force and lerping position? Possible unity bug? 0 Answers
Moving a player with Rigidbody 2 Answers