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;
    }

}

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;
    }
}