如果光线投射没有击中障碍物,只看球员

时间:2013-09-23 16:08:10

标签: c# unity3d artificial-intelligence

我在这里有一个可用的避让脚本,但我唯一的问题是它总是试图查看播放器,即使它绕过障碍物导致它抖动。我想知道如果光线投射没有碰到障碍物但是让它同时向玩家移动,我怎么能只看到玩家。任何帮助,将不胜感激, 目前正在使用unity3D

using UnityEngine;
using System.Collections;

public class enemyAI : MonoBehaviour
{   
    public Transform target;
    public float moveSpeed;
    public float rotationSpeed;
    public float minDistance = 0.5f;
    public static enemyAI enemyAIself; 
    RaycastHit hit;

    void Awake()
    {
       enemyAIself = this;   
    }
    void Start () 
    {
       GameObject goTo = GameObject.FindGameObjectWithTag("Player");
       target = goTo.transform;
    }

    void Update () 
    {
       Vector3 dir = (target.position - transform.position).normalized;
       if (Physics.Raycast(transform.position, transform.forward, out hit, 5.0f, (1<<8)))
       {
       Debug.DrawRay(transform.position, hit.point, Color.blue);
       dir += hit.normal  * 50;
       }

       Vector3 leftR = transform.position;
       Vector3 rightR = transform.position;

       leftR.x -= 2;
       rightR.x += 2;

       if (Physics.Raycast(leftR, transform.forward, out hit, 5.0f, (1<<8)))
       {
        Debug.DrawRay(leftR, hit.point, Color.blue);
          dir += hit.normal  * 50;
       }

       if (Physics.Raycast(rightR, transform.forward, 5.0f, (1<<8)))
       {
       Debug.DrawRay(rightR, hit.point, Color.blue);
       dir += hit.normal  * 50;

       } 
       Quaternion rot = Quaternion.LookRotation(dir);
       transform.rotation = Quaternion.Slerp(transform.rotation, rot, rotationSpeed * Time.deltaTime);

       if (Vector3.SqrMagnitude(target.position - transform.position)> (minDistance *  minDistance))
       {
         //move towards the target
         transform.position += transform.forward * moveSpeed * Time.deltaTime;
       }

    }


}

1 个答案:

答案 0 :(得分:0)

如果光线投射没有撞到障碍物但是让它在你可能的同时向玩家移动,那么只能看看玩家

在生命点创建临时航点(Vector3)

沿着障碍物移动航点,直到它的光线投射停止撞击障碍物

转移到此航路点

继续走向播放器