how can i determine the back of object?
i want to make something like a backstab, it means when i am behind some object, i can deal more damage. any help would be appreciated
$$anonymous$$y answer wasn't totaly correct so i redo it : If your enemy and your player are looking in the same direction and your player hit the enemy, then he must be in his back. You can so do the project of the two forward (Vector3.Project(enemy.transform.forward, player.transform.forward), and what interest us is the z value: if it is 1, the player and the enemy are strictly looking in the same direction. if it > 0.5f, they are looking at the same direction with a margin of 45° if it is 0, they are looking in perpendicular directions if it is -1, the are looking in opposite directions.
So you can do something like : if(Vector3.Project(enemy.transform.forward, player.transform.forward).z>0.3f) CriticalHit();
Answer by ScaniX · Aug 26, 2016 at 01:13 PM
If "other" is the enemy gameObject, you can calculate the angle between the enemies forward vector and the delta vector. Comparing just the both forward vectors would not actually say anything about your position relative to the enemy. You will have the same angle if you are standing right in front of or behind the enemy.
Vector3 delta = transform.position - other.transform.position;
Vector3 angle = Vector3.Angle(other.transform.forward, delta);
float backstabbiness = Mathf.Abs(angle) / 180f; // Mathf.Abs() is probably not necessary
The closer the angle gets to 180° the closer you are to be directly behind him. A backstabbiness value of > 0.7f should take him out instantly. ;)
thanks I'm still experimenting with what you posted since I have an Isometric view.
Just change the Vector3 angle
to float angle
Answer by DavidWatts · Aug 26, 2016 at 01:16 PM
you can take the dot product between the enemies forward vector and a position in space. if the dot product is greater than 0 then the position is in front and if its less than 0 then its behind.
Your answer
![](https://koobas.hobune.stream/wayback/20220612091607im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
c# help with rotate and rotatearound 0 Answers
iron sight using inverse kinematic 0 Answers
How to disable diagonal Vector3 movement of non-player object using MoveTowards? 1 Answer
prob to define a condition to get out of a search position loop for obstacle avoidance 0 Answers
Creating a new vector3 2 Answers