阻止光线投射穿过墙壁

时间:2014-11-26 08:21:14

标签: c# artificial-intelligence raycasting

所以我正在制作一个你必须能够躲避敌人的游戏。当他看到我时,敌人看到我并跟着我,但是当我在我们之间放一堵墙时,光线投射忽视了墙壁并使敌人仍然跟着我。我没有使用图层,因为我真的不明白他们做了什么。我甚至不知道我是否应该使用它们。这是我的敌人ai脚本,在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 个答案:

答案 0 :(得分:0)

您没有检查光线投射所击中的物体是否实际上是玩家。所以你的代码基本上说:“如果这个光线投射击中任何一个,请关注播放器。”要检查光线投射命中哪个对象,只需创建一个RaycastHit对象并将其作为参数添加到Physics.Raycast调用中。所以你的第一个RayCast看起来像这样:

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