- Home /
getting the direction of object
Im using the following script to apply damage to my enemies
if(Input.GetButton("Attack")){
Collider[] cols = Physics.OverlapSphere(transform.position, rang);
foreach (Collider col in cols){
if(col.tag == "Enemy"){
// if object has the right tag...
// assuming the enemy script is called EnemyScript
EnemyHealth script = col.GetComponent<EnemyHealth>();
script.AddjustCurrentHealth(-10); // apply damage 5
This works well, but i don't want to attack the enemy if it is behind me. how cab i get the rotation of the object so i can use it in an if statement, or is there another way to do it? Please Help
Answer by DaveA · Nov 03, 2011 at 09:35 PM
If your player has a camera you could try this: http://unity3d.com/support/documentation/ScriptReference/Camera.WorldToViewportPoint.html and make sure his viewpoint coords are between 0 and 1 (or whatever field of view you like).
If not, you could create a vector from player to enemy, and cross-product that with the player's forward vector. If the resulting Y component points up, he's in front, if down, he's behind (or something like that)
Your answer
Follow this Question
Related Questions
How to have an object moving to one direction and rotate it whitous affecting the direction 1 Answer
Object problem 1 Answer
How to make two rays with different offset follow objects rotation? 1 Answer
How do I rotate a circle of objects so that a specific object is in front of the camera? 2 Answers