Knockback direction always the same
Whenever my player is attacked by an enemy I want him to be knocked in the direction away from the enemy. Instead he always get's knocked the way he isn't facing.
This is the part of the script which controls the knockback:
if (other.gameObject.name == "enemySword" && !damaged){
var target : Transform = other.transform;
health -= 10;
dir1 = transform.position - target.position.normalized;
rb.AddRelativeForce(dir1 * thrust);
damaged = true;
damageWait();
}
}
Answer by yanhui · May 25, 2016 at 04:31 PM
you should change code to this :
//dir1 = transform.position - target.position.normalized;
dir1 = (transform.position - target.position).normalized;
I tried that earlier and the character no longer gets knocked back at all.
Answer by Griffo · May 25, 2016 at 04:37 PM
Or change line 5 to ..
rb.AddRelativeForce(-Vector3.forward * thrust);
This makes the character not get knocked back at all.
Although technically it should produce the same problem I'm having. With the character getting knocked backwards even if he gets hit from a different direction.
Your answer
![](https://koobas.hobune.stream/wayback/20220612081545im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make object land on certain point?,How to make object land on certain point?? 0 Answers
Vector3 Help - Calculating velocity toward a point 1 Answer
How do I ignore gravity when using Rigidbody and always have the same jump force? 1 Answer
Is there a way to "rotate" the direction of addForce (without using addRelativeForce)? 0 Answers