Question by
Mystic_Quest · Feb 05, 2017 at 01:48 AM ·
2dtopdownknockback
Top down zelda-like knockback working only on y axis
Tried using these (not together) in the OnTriggerEnterCollider2D, both work only on the y axis, not on the x. What am I doing wrong?
1.
Vector2 directionVector = other.transform.position - transform.position;
Rigidbody2D body = other.GetComponent<Rigidbody2D>();
if (body != null)
{
float forceMagnitude = 0.8f;
ForceMode2D mode = ForceMode2D.Impulse;
body.AddForce(directionVector * forceMagnitude, mode);
}
2.
Vector3 direction = (other.transform.position - transform.position).normalized;
body = other.GetComponent<Rigidbody2D>();
body.AddForce(direction * 10);
Comment
Answer by BlockFade · Feb 06, 2017 at 07:50 PM
You could use iTween! Use this code:
void OnColliderEnter2D(collision col) {
if(col.gameObject.tag == "yourTagHere") {
iTween.MoveTo(GameObject target, Vector3 position, float timeSpan);
}
}
BTW position is the position to move to also you should use a rather short timespan to add that extra kick.
Thanks I'll keep iTween in $$anonymous$$d for future projects, but on this one I don't want to use any external plugins because it's for my university.
Your answer
![](https://koobas.hobune.stream/wayback/20220612110345im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D Knockback problem 2 Answers
Weird infinite loop-or just long loading time 2 Answers
2D TopDown rotating a gun according to its parent position, ON MOBILE, not PC, 0 Answers
Turnbased topdown 2D mouse movement 0 Answers
RigidBody.AddForce() doesn't apply 2 Answers