仅当光线投射没有击中障碍物时,才查看玩家
本文关键字:玩家 障碍物 光线投射 | 更新日期: 2023-09-27 18:32:30
我在这里得到了一个有效的回避脚本,但我遇到的唯一问题是它总是试图看着玩家,即使它绕过障碍物导致它抖动。我想知道如何让它只看玩家,如果光线投射没有击中障碍物,但同时让它向玩家移动。任何帮助将不胜感激,当前使用 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;
}
}
}
如果光线投射没有击中障碍物,则只看玩家,但同时让它向玩家移动
,你可以在命中点创建临时航点 (Vector3)
沿障碍物移动航点,直到来自它的光线投射停止击中障碍物
移动到此航点
继续走向玩家