x


Raycast not working properly

I'm trying to create a raycast for my enemy so if the player is behind a wall, the enemy will not attack...this is what im currently using inside my update function:

var dist = lookAtTarget.position - transform.position;

var hit : RaycastHit;
if(Physics.Raycast(transform.position, dist,hit))
{
    if(hit.collider.CompareTag("Player"))
    {
        Attack();
    }
    else
    {
        return false;
    }

        Debug.DrawLine (transform.position, hit.point);
}

The problem im having is that when i play the game, if the player is in range of the enemy but standing still, the enemy will not attack...but if i start moving, thats when the enemy starts to attack...All im trying to get accomplished is to have the enemy disengage when the player is either behind a wall or out of range with the use of raycast...I also tried using linecast and i was getting the same result...

more ▼

asked Oct 30 '10 at 01:15 AM

John 17 gravatar image

John 17
17 4 4 10

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You make the call to Physics.Raycast() outside of any function. I think you should move it to Update() to let the enemy constantly check for the player instead of just once. Still I can't explain why the enemy starts to attack when the player moves.

more ▼

answered Oct 30 '10 at 08:17 AM

lhk gravatar image

lhk
520 14 15 24

The call to raycast IS in my update function but still only attacks when the player is moving....please anybody

Oct 30 '10 at 04:18 PM John 17
(comments are locked)
10|3000 characters needed characters left
if(Physics.Raycast(transform.position, dist*1.1,hit))

untested

more ▼

answered Dec 24 '10 at 02:41 AM

cupsster gravatar image

cupsster
385 6 8 15

(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:

x2484
x1525
x651

asked: Oct 30 '10 at 01:15 AM

Seen: 1034 times

Last Updated: Oct 30 '10 at 01:15 AM