x


Raycast problems in my AI

hey, I try to make this AI working for quite a while now.. The problem i have with it is, that a the raycast always returns positiv, even if the Object it hits is not my player!! I dont know what I am doing wrong! this is my whole AI... I know that it is not the best jet ;P

var target : Transform; 
var moveSpeed = 3;
var rotationSpeed = 3; 
var hunt : boolean = false;
var myTransform : Transform; 
var distance : int;
var inSight : boolean = false;
var runSpeed = 9;
function Awake()
{
    myTransform = transform;
}

function Start()
{
     target = GameObject.FindWithTag("Player").transform; 

}

function Update () {

    dist = (transform.position - target.position).magnitude;
    if(dist < distance){
        hunt = true;
    }
    else{
        hunt = false;
    }
    var hit : RaycastHit;
    var direction : Vector3;
    direction =target.position - transform.position;
    if(Physics.Raycast(transform.position , direction, hit)) {
        if(hit == GameObject.FindWithTag("Player")) {
            var InSight = hit.distance;
        }
    }

    if(InSight <= distance){
        inSight =true;
    }
    else{
        inSight = false;
    }

    if(hunt == true){

    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);


    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
    }
    if(inSight == true){
        myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
    }
    if (Player_Simply.running == true) {
        myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
    myTransform.position += myTransform.forward * runSpeed * Time.deltaTime;
    }



}
more ▼

asked Oct 19 '10 at 02:08 PM

Juri gravatar image

Juri
68 15 18 25

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

1 answer: sort voted first

Physics.Raycast will raycast against every object within the layers specified by the cullingMask which you leave as kDefaultRaycastLayers, not just against just your player as your question states that you are expecting.

  • hit is a RaycastHit, not a GameObject. hit == GameObject.FindWithTag("Player") will always return false. You should probably use hit.collider.gameObject == GameObject.FindWithTag("Player") which will actually work.
  • You declare the InSight variable within the scope of the two if statements and then try to access it in a different scope. This will not work.
  • Why do you even bother with GameObject.FindWithTag("Player") within your raycast if when you store the transform of this GameObject in target?
  • You check a boolean condition to set hunt, but you only use it in one place. Why not just check the condition itself? Isn't it the same with inSight?
  • You calculate target.position - transform.position up to five times - why?
  • You do essentially the same thing in all three if statements. Why?
  • You're not supposed to pass Slerp/Lerp a deltaTime, but an incremental time value unless you want it to always look some small amount towards the target. Since you are also slerp/lerping from a constantly changing rotation to a potentially constantly changing rotation by some tiny control amount, you will never actually reach the target unless your frame rate should slow significantly (in your case to rotationSpeed frames per second).

This does what it seems you are trying to do and should actually accomplish it (Note that the Slerp here is not framerate dependent anymore):

var target : Transform;
var huntRange : float = 20.0f;
var rotationSpeed : float = 0.1f;
var moveSpeed : float = 3.0f;
var runSpeed : float = 5.0f;
private var speed : float;

function Start() {
    target = GameObject.FindWithTag("Player").transform; 
}

function Update() {
    var hit : RaycastHit;
    var direction : Vector3 = target.position - transform.position;

    if(direction.magnitude < huntRange
        && (Physics.Raycast(transform.position , direction, hit)
            && hit.collider.gameObject == target.gameObject)) {

        //Shorter than writing 2-3 ifs that do the same thing
        if(Player_Simply.running) speed = runSpeed;
        else speed = moveSpeed;

        transform.rotation = Quaternion.Slerp(transform.rotation,
                                    Quaternion.LookRotation(direction),
                                    rotationSpeed);
        transform.position += transform.forward * speed * Time.deltaTime;
    }
}
more ▼

answered Oct 19 '10 at 03:56 PM

skovacs1 gravatar image

skovacs1
10k 11 25 91

thanks for ur great help, but i tried ur script, and the only thing that my hunter is doing is forward! he is not looking at my player nor is he running after him! is it a problem with the script or is it me??? so basicly the new script is doing less then my old one... but ur it looks a lot better :P (this is not supposed to say, that i don't appreciate ur help)

Oct 20 '10 at 11:30 AM Juri

Yeah. Sorry about that. Forgot the transform. before rotation - I'm a little surprised that wasn't an error actually.

Oct 20 '10 at 02:03 PM skovacs1

where is a transform. missing???

Oct 20 '10 at 02:32 PM Juri

It was missing. See where it says I edited the post 2 hours ago? That's when I fixed it.

Oct 20 '10 at 04:20 PM skovacs1

ok so now its doing everything that it should, but it is ignoring everything in its way. when i put a box in its way with a collider on it, the hunter is just going through it. maybe u can tell me a way, so i can send u my project by now i think that there is a problem in a other script! thanks

Oct 22 '10 at 09:18 AM Juri
(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:

x5049
x1863
x1524
x954

asked: Oct 19 '10 at 02:08 PM

Seen: 1695 times

Last Updated: Oct 19 '10 at 02:08 PM