ForceMode2D.Impulse problem
How do I stop my player from getting dragged back after collision?
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Bouncy object")
GetComponent<Rigidbody2D>().AddForce(transform.right * 15, ForceMode2D.Impulse);
}
Please explain what you mean by "getting dragged back after collision". What is "dragged back"?
like it's being pulled back by a magnet! Which I don't want, I just want my player to get push back by a tagged object and stays there
Answer by davidgab98 · Mar 31, 2020 at 08:38 PM
// You can set its velocity as zero:
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Bouncy object")
GetComponent<Rigidbody2D>().velocity = Vector2.zero;
}
// Also you can change to true the variable isKinematic from player RigidBody2D:
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Bouncy object")
GetComponent<Rigidbody2D>().isKinematic = true;
}
Answer by streeetwalker · Mar 30, 2020 at 03:48 PM
@Creative Inventory , I'm not sure why you're downvoted for your question, but you don't provide enough of your code to tell what is going on.
For sure you code works as it is written. When your objects touch each other, a force of 15 is applied to the positive x direction.
So if that is not what you want to have happen, you must have other forces that act on the object, or you are trying to position the object in some other way (like setting the transform.position - which you should not do if you are using the rigidbody physics engine.)
So we can't tell what you are trying to do in the code. You want this to act as a booster for the player? Or you want the player collide and bounce off some object?
if you want the player to bounce off, you should probably not use a trigger collider to do that - just turn off the trigger and let the object collide and bounce off.
You have to explain how things are expected to work in detail. What objects are involved, what the set up is, and what is the rest of the code!