Stop raycasts from going through walls

5.3k Views Asked by At

So Im making a game in which you have to be able to hide from the enemy. The enemy sees me and follows me when he sees me but when I put a wall between us, the raycast ignores the wall and makes the enemy still follow me. Im not using layers because I dont really understand what they do. Also I dont even know if I should use them. Here is my enemy ai script, in c#:

using UnityEngine; 
using System.Collections;

public class EnemyAI : MonoBehaviour { 

private GameObject playerTarget;
private SphereCollider col;
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public int maxDistance;
private Transform myTransform;
public int RayDist;
public Vector3 position;

public float RayDir;


void Start ()
{
    playerTarget = GameObject.FindGameObjectWithTag ("Player");
    col = GetComponent<SphereCollider>();
    myTransform = transform;
    GameObject go = GameObject.FindGameObjectWithTag("Player");
    character = GetComponent<CharacterController>();
    target = go.transform;  

    var diagonal = transform.TransformDirection(1, 0, 1); 
    position = transform.position-transform.right*3;
}

CharacterController character;

void Update(){

}


void OnTriggerStay (Collider other)
{

    if(other.gameObject == playerTarget)

    {   

        RaycastHit hit;

        Vector3 dir = target.position - myTransform.position; dir.y = 0; 



        Vector3 fwd = transform.TransformDirection(Vector3.forward * RayDist);

        if(Physics.Raycast(transform.position, fwd, out hit,10))
        {
            if (hit.transform.gameObject == playerTarget)
            {
            Debug.Log ("Raycast entered ");
            myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime); 
            if (dir.magnitude > maxDistance) 
            { 
                character.Move(myTransform.forward * moveSpeed * Time.deltaTime); 
            } 
            }
        }
        Debug.DrawRay (transform.position, fwd, Color.green);

        Vector3 slightright = transform.TransformDirection(new Vector3(RayDir,0,2) * RayDist);
        if(Physics.Raycast (transform.position, slightright, out hit, 10)){
            if (hit.transform.gameObject == playerTarget)
            {
            Debug.Log ("slightright Raycast entered");
            myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime); 
            if (dir.magnitude > maxDistance) 
            { 
                Debug.Log ("Left Raycast move function entered");
                character.Move(myTransform.forward * moveSpeed * Time.deltaTime); 
            } 
        }
        }
        Debug.DrawRay (transform.position, slightright, Color.magenta);

        Vector3 right = transform.TransformDirection(new Vector3(RayDir,0,1) * RayDist);

        if(Physics.Raycast (transform.position, right, out hit, 10)){
            if (hit.transform.gameObject == playerTarget)
            {
            Debug.Log ("Right Raycast entered");
            myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime); 
            if (dir.magnitude > maxDistance) 
            { 
                character.Move(myTransform.forward * moveSpeed * Time.deltaTime); 
            } 
        }
        }
        Debug.DrawRay (transform.position, right, Color.blue);

        Vector3 left = transform.TransformDirection(new Vector3(-RayDir,0,1) * RayDist);
        if(Physics.Raycast (transform.position, left, out hit, 10)){
            if (hit.transform.gameObject == playerTarget)
            {
            Debug.Log ("Left Raycast entered");
            myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime); 
            if (dir.magnitude > maxDistance) 
            { 
                Debug.Log ("Left Raycast move function entered");
                character.Move(myTransform.forward * moveSpeed * Time.deltaTime); 
            } 
        }
        }
        Debug.DrawRay (transform.position, left, Color.red);

        Vector3 slightleft = transform.TransformDirection(new Vector3(-RayDir,0,2) * RayDist);
        if(Physics.Raycast (transform.position, slightleft, out hit, 10)){
            if (hit.transform.gameObject == playerTarget)
            {
            Debug.Log ("Left Raycast entered");
            myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime); 
            if (dir.magnitude > maxDistance) 
            { 
                Debug.Log ("Left Raycast move function entered");
                character.Move(myTransform.forward * moveSpeed * Time.deltaTime); 
            } 
        }
        }
        Debug.DrawRay (transform.position, slightleft, Color.cyan);
    }


}

}

1

There are 1 best solutions below

12
On BEST ANSWER

You are not checking if the object that is hit by the ray cast is actually the player. So your code basically says: "If this raycast hits ANYTHING, follow the player." To check which object is hit by the raycast, simply create a RaycastHit object and add that as a parameter to the Physics.Raycast calls. So your first RayCast would look like this:

RaycastHit hit;
if(Physics.Raycast(transform.position, fwd, out hit ,10))
{
    if (hit.transform.gameObject == playerTarget)
    {
        Debug.Log ("Raycast entered ");
        myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(dir), rotationSpeed * Time.deltaTime); 
        if (dir.magnitude > maxDistance) 
        { 
            character.Move(myTransform.forward * moveSpeed * Time.deltaTime); 
        }
    }
}