x


Raycast intersection

i'm trying to create my own raycast AI and I want the enemy to stop following the player when something gets between them. here is my code so far; it moves by its self and it follows the player when they get into range:

var speed : float = 6;
var rotateSpeed : float = 10;
private var direction : float;
var forwardRay : float = 5;
var leftRay : float = 5;
var rightRay : float = 5;
var player : Transform;
var MaxDistance : int = 30;
var MinDistance : int = 5;
var hidden : boolean = true;

function Update () {

Patrol();

var distance : Vector3 = player.position - transform.position;
    //if player is in range, enemy will follow
    if(distance.magnitude < MaxDistance){
        Debug.DrawLine(transform.position, player.position);
        //if something is 4 units away from the enemy, the player becomes hidden
        if(Physics.Raycast(transform.position, player.position, 4)){
            hidden = true;
        }else{
            hidden = false;
        }
    }else{
        hidden = true;
    }
}

function Patrol(){
    //if the player is hidden, the enemy will patrol
    if(hidden){
        //if there is nothing within range, then move forward
        if(!Physics.Raycast(transform.position, transform.forward, forwardRay)){
            transform.Translate(Vector3.forward * speed * Time.smoothDeltaTime);
        }else{
            //if there is something to the left, then direction = 1
            if(Physics.Raycast(transform.position, -transform.right, leftRay)){
                direction = 1;
            //if there is something to the right, the direction = -1
            }else if(Physics.Raycast(transform.position, transform.right, rightRay)){
                direction = -1;
            //if there is nothing to the left or right, then direction = 1
            }else{
                direction = 1;
            }
            //rotate 90 degrees in the direction (1 = right, -1 = left)
            transform.Rotate(Vector3.up, 90 * rotateSpeed * direction);
        }
    //if the player is not hidden...
    }else if(!hidden){
        var distance : Vector3 = player.position - transform.position;

        //move towards player
        velocity = distance.normalized * speed;
        rigidbody.velocity = velocity;
    }
}
more ▼

asked Dec 14 '10 at 09:46 AM

MonkeyAssassin8 gravatar image

MonkeyAssassin8
271 35 38 43

What was your question exactly?

Dec 14 '10 at 10:45 AM Jesse Anders

it's ok, i figured it out, i used a LineCast instead of a Raycast to see if anything got in the way.

Dec 14 '10 at 12:50 PM MonkeyAssassin8
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

you need to ask a question, but does this answer what you meant to ask?

When the player is not hidden, raycast towards the player from the enemy. only if the player is hit move toward him. The trick is to use RaycastHit to see what the raycast hit.

else if(!hidden)
{
    // distance is also the direction of our player from us in this case.
    var distance: Vector3 = player.position - transform.position;

    var hit : RaycastHit;

    if(Physics.Raycast(transform.position, playerDir, hit))
    {
        // look for what makes you player unique, ie: tag, layer, name
        if(hit.transform.tag == "someRandomPlayerQualifier")
        {
            //move towards player
            velocity = distance.normalized * speed;
            rigidbody.velocity = velocity;
        }
    }
}

Hope's this help

Links:Physics.Raycast RaycastHit

more ▼

answered Dec 14 '10 at 12:58 PM

Usul gravatar image

Usul
452 3 5 12

I'd already figured it out, but thanks a lot anyway.

Dec 15 '10 at 05:17 AM MonkeyAssassin8
(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:

x151
x69
x25

asked: Dec 14 '10 at 09:46 AM

Seen: 869 times

Last Updated: Dec 14 '10 at 09:46 AM