- Home /
RigidBody2D pushed away on opposite direction
Well, I guess this is a basic one but I can't figure it out.
I'm trying to knock back an enemy and push him away a bit when I struck him. This is my code: public class PushObjectAway : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
Vector2 dir = transform.position - other.transform.position;
dir.Normalize();
if (other.gameObject.tag == "Enemy")
{
Rigidbody2D rbOther = other.gameObject.GetComponent<Rigidbody2D>();
rbOther.AddForce(dir * 2000 * Time.deltaTime);
Debug.Log(rbOther);
}
}
}
The Debug message is working, I'm detecting the right collider but the target object just won't move an inch. I've also tried this: Rigidbody2D pushing in opposite direction (and other solutions) but the target just won't move.
Any ideas?
For starters, I believe Rigidbody.AddForce doesn't require you to multiply by Time.deltaTime, so try removing that.
@JasonSic Thanks for your answer, I've already tried that (among other formulations of the AddForce line...)
@$$anonymous$$elv$$anonymous$$ay @aditya the Rigidbody2D was kinematic indeed, you saved me with that one (I was sure I've tried the $$anonymous$$inematic box as well, apparently I didn't) now is working! Thank you very much for your help guys!
That's good to hear. Note that you can still make it move but you need to modify its velocity which is really simple:
rbOther.velocity = rbOther.velocity + (dir * <somevalue>);
Answer by MelvMay · Apr 15, 2016 at 06:37 AM
For starters, there might not be another Rigidbody2D as the Collider2D might be static so you should check for that.
The force you're adding isn't an impulse and is already scaled by time when it is applied during the next fixed-update so you're scaling by time twice so this is going to be a very small force.
Use an Impulse force (which is instantaneous) instead. When using Rigidbody2D.AddForce specify the ForceMode2D argument as ForceMode2D.Impulse (you'll definitately want to reduce the 2000 constant then).
@$$anonymous$$elv$$anonymous$$ay Thank you for answer. I've tried what you propose but still no luck. Here's the line I've changed:
rbOther.AddForce(dir * 20, Force$$anonymous$$ode2D.Impulse);
I made sure the target has a RigidBody2D (and the debug line is confir$$anonymous$$g so):
The BoxCollider2D in the player has a rigidbody2D as well and the code attached, the behavior I expect is when the player moves towards the NCP and the NCP enters the collider, the NCP gets pushed away. Last Note: the Collider2D Hitbox of the player is set to Trigger
Check what force you're adding by outputting to the console
Check the velocity before you add force and afterwards (should change)
Check that nothing else is resetting the velocity to zero
Check that the $$anonymous$$ass isn't some crazy high value
Check that RB constraints on XY are not checked
Check you're not setting Transform.position overriding physics position
I've checked all those points. Right now the NCP is just a dummy GameObject I've created to test the script so it doesn't have any Transform settings or crazy controllers that could override the physics. Not quite sure how to debug the force I'm adding, I've debugged the velocity getting (0,0) as result (after the addForce line)
@$$anonymous$$elv$$anonymous$$ay This one is really trying to drive me nuts :) So, as I've said before, the "push away" code is now working... but only in the editor :( When I try the prototype on my Android the target won't move when it gets pushed, it only moves if its facing the opposite direction of the player (when we hit him on the back) weird thing is that this issues is not present in the editor.
And even weirder... after making the build the bug appears in the editor too, is very random, I'm having a hard time to find the reason
Answer by aditya · Apr 15, 2016 at 07:04 AM
try rbOther.AddForce(dir * 2000, ForceMode.Impulse);
, and please apply force in FixedUpdate
@aditya I've tried that, not luck, here's the code with the modifications you've suggested: using UnityEngine; using System.Collections;
public class PushObjectAway : $$anonymous$$onoBehaviour {
private Vector2 dir;
private Rigidbody2D rbOther;
void OnTriggerEnter2D(Collider2D other)
{
dir = transform.position - other.transform.position;
dir.Normalize();
if (other.gameObject.tag == "Enemy")
{
rbOther = other.gameObject.GetComponent<Rigidbody2D>();
}
}
void FixedUpdate()
{
rbOther.AddForce(dir * 2000, Force$$anonymous$$ode2D.Impulse);
Debug.Log(rbOther);
}
}
will you please tell me if this code is even applying any force to enemy or not, plus use above code like this ....
private Vector2 dir;
private Rigidbody2D rbOther;
private bool applyForce;
void OnTriggerEnter2D(Collider2D other)
{
dir = other.transform.position - transform.position;
dir.Normalize();
if (other.gameObject.tag == "Enemy")
{
rbOther = other.gameObject.GetComponent<Rigidbody2D>();
applyForce = true;
}
}
void FixedUpdate()
{
if(applyForce){
applyForce = false;
rbOther.AddForce(dir * 2000, Force$$anonymous$$ode2D.Impulse);
Debug.Log(rbOther);
}
}
Thank you for helping me out, I'm going nuts with this one. So, here's the updated code:
using UnityEngine;
using System.Collections;
public class PushObjectAway : $$anonymous$$onoBehaviour {
private Vector2 dir;
private Rigidbody2D rbOther;
private bool applyForce;
void OnTriggerEnter2D(Collider2D other)
{
dir = other.transform.position - transform.position;
dir.Normalize();
if (other.gameObject.tag == "Enemy")
{
rbOther = other.gameObject.GetComponent<Rigidbody2D>();
applyForce = true;
}
}
void FixedUpdate()
{
if (applyForce)
{
applyForce = false;
rbOther.AddForce(dir * 2000, Force$$anonymous$$ode2D.Impulse);
Debug.Log(rbOther);
Debug.Log(rbOther.velocity);
}
}
}
Still getting nothing :( The two debugs lines are throwing this:
Player_1 (UnityEngine.Rigidbody2D) UnityEngine.Debug:Log(Object)
and
(0.0, 0.0) UnityEngine.Debug:Log(Object)
Your answer
![](https://koobas.hobune.stream/wayback/20220612074826im_/https://answers.unity.com/themes/thub/images/avi.jpg)