x


issue with raycast behavior

I am building a simple line of sight script in unity using javascript and on a flat plane it works just fine. However on terrain i get some issues mainly the object can't seem to see the player. Is there an adjustment i can make for this?

right now this is the code i have for detecting the player:

function CanSeePlayer() : boolean
{
    var hit : RaycastHit;
    var rayDirection = Target.transform.position - transform.position; 

    // If the Target is close to this object and is in front of it, then return true
    if((Vector3.Angle(rayDirection, transform.forward)) < (fieldOfViewRange*2) && (Vector3.Distance(transform.position, Target.transform.position) <= attackRange))
    {
        inAttackRange = true;
        return true;
    }

    else
    {
       inAttackRange = false;
    }

    if((Vector3.Angle(rayDirection, transform.forward)) < fieldOfViewRange)
    { // Detect if player is within the field of view
        if (Physics.Raycast (transform.position, rayDirection, hit, SightRange)) 
        {
            if (hit.collider.gameObject == Target) 
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

any help would be great.

thanks.

more ▼

asked Apr 24 '12 at 02:48 PM

kievar1983 gravatar image

kievar1983
66 9 11 15

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

1 answer: sort voted first

The ray is probably coming from your feet, which means it hits the ground if the ground has the smallest of bumps on it. To test, can add a Debug.DrawRay(transform.position,rayDrection);. You can only see it in scene mode, and it can be "off" from your real raycast, but it gives a hint.

Just pick a spot on the player where the "eyes" are, and a spot to look at on the enemy (if you use Target.position then you're checking if you can see its feet, so small bumps can still block you):

var playerEyes : Vector3 = transform.poistion+Vector3.up*1.2;
var enemyChest : Vector3 = Target.position+Vector3.up*0.8;
rayDirection = enemyChest - playerEyes;

This is crude, but works for manlike things (I'm saying the eyes are always 1.2 meters up from the feet.) You could child an empty in front of the player's head to be the eyes, and use: playerEyes=transform.Find("eyes").position;

more ▼

answered Apr 24 '12 at 03:37 PM

Owen Reynolds gravatar image

Owen Reynolds
11.3k 1 7 45

this kind of worked, but still same results the enemy only seems to see me when i back up and not move towards it. on a flat plane they see me all the time and i've checked the ray and it's a line directly to the player head and nothing is in the way.

anything else you can think of that may be causing issues?

Apr 24 '12 at 05:32 PM kievar1983

Figure out what the raycast is telling you. Maybe toss in a global string and set it to hit.transform.name if you get a hit, or "" if you miss. That should tell you exactly why it can't see you.

Apr 25 '12 at 12:23 AM Owen Reynolds
(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:

x5071
x3455
x1528

asked: Apr 24 '12 at 02:48 PM

Seen: 498 times

Last Updated: Apr 25 '12 at 12:23 AM