0
我在這裏有一個迴避腳本,但我唯一的問題是,它總是試圖看着玩家,即使它繞過障礙導致它抖動。我想知道如果我的射線沒有碰到障礙物,而是讓它在同一時間向玩家移動,我只能看看玩家。任何幫助,將不勝感激, 目前與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;
}
}
}
感謝您的回覆,iam試圖鍛鍊如何創建一個航點atm – user2807944