x


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

more ▼

asked Nov 03 '11 at 09:14 PM

Babilinski gravatar image

Babilinski
119 40 42 43

You could use SphereCast.

Nov 03 '11 at 09:17 PM OrangeLightning
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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)

more ▼

answered Nov 03 '11 at 09:35 PM

DaveA gravatar image

DaveA
26.8k 153 171 257

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2249
x1123
x296
x38
x28

asked: Nov 03 '11 at 09:14 PM

Seen: 1201 times

Last Updated: Nov 03 '11 at 09:35 PM