【问题标题】:only look at the player if the raycast is not hitting the obstacle仅在光线投射未击中障碍物时才查看播放器
【发布时间】:2013-09-23 16:08:09
【问题描述】:

我在这里有一个有效的回避脚本,但我遇到的唯一问题是它总是试图看着玩家,即使它绕过障碍物导致它抖动。我想知道如果光线投射没有撞到障碍物但让它同时向玩家移动,我怎么能让它只看玩家。任何帮助,将不胜感激, 目前正在使用 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;
       }

    }


}

【问题讨论】:

    标签: c# unity3d artificial-intelligence


    【解决方案1】:

    如果光线投射没有击中障碍物,让它只看玩家,但同时让它朝着玩家移动,你可以

    在命中点创建临时航路点(Vector3)

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

    移动到这个航点

    继续走向玩家

    【讨论】:

    • 感谢您的回复,我正在尝试锻炼如何创建航点 atm
    猜你喜欢
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    相关资源
    最近更新 更多