C# Unity 2018-NPC在射程内顺利转向面对玩家
使用当前代码,NPC将检测并转向播放所需动画的播放器。但是,当玩家在NPC范围内绕NPC行走时,NPC只会捕捉到面对玩家的方向,而不会继续旋转 我想修改这一点,这样NPC就可以在玩家在对撞机的射程内持续、平稳地转向角色。我想这可能与void update有关,但由于要打开的函数当前位于C# Unity 2018-NPC在射程内顺利转向面对玩家,c#,unity3d,C#,Unity3d,使用当前代码,NPC将检测并转向播放所需动画的播放器。但是,当玩家在NPC范围内绕NPC行走时,NPC只会捕捉到面对玩家的方向,而不会继续旋转 我想修改这一点,这样NPC就可以在玩家在对撞机的射程内持续、平稳地转向角色。我想这可能与void update有关,但由于要打开的函数当前位于onTriggerEnter中,对于像我这样的初学者来说,这有点让人困惑 public class helloTrigger : MonoBehaviour { public Animator anim;
onTriggerEnter
中,对于像我这样的初学者来说,这有点让人困惑
public class helloTrigger : MonoBehaviour
{
public Animator anim;
public CharacterController player;
public Transform Player;
void Start()
{
player = GameObject.FindObjectOfType<CharacterController>();
anim = GetComponent<Animator>();
}
void OnTriggerEnter(Collider other)
{
if (other.tag != "Player") return;
anim.Play("Hello");
Vector3 lookAt = Player.position;
lookAt.y = transform.position.y;
transform.LookAt(lookAt);
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
anim.Play("Idle");
}
}
}
公共类helloTrigger:MonoBehavior
{
公共动画;
公共人物控制器;
公共转换播放器;
void Start()
{
player=GameObject.FindObjectOfType();
anim=GetComponent();
}
无效对撞机(对撞机其他)
{
如果(other.tag!=“Player”)返回;
动漫游戏(“你好”);
Vector3注视=玩家位置;
lookAt.y=transform.position.y;
变换。注视(注视);
}
void OnTriggerExit(碰撞器其他)
{
如果(other.tag==“玩家”)
{
动漫游戏(“空闲”);
}
}
}
如果希望游戏对象平滑地面对另一个游戏对象,请使用四元数.LookRotation
计算目标旋转,然后使用四元数.Lerp
在当前旋转和使用四元数.LookRotation
计算的目标旋转之间进行Lerp。最后,随着时间的推移做一些练习。请参阅文章以了解旋转上的lerping是如何工作的。您可以在Update
函数中执行此操作,但我将在协同程序函数中使用它,因为它提供了更多的控制
简单介绍平滑功能:
IEnumerator LookAtSmoothly(Transform objectToMove, Vector3 worldPosition, float duration)
{
Quaternion currentRot = objectToMove.rotation;
Quaternion newRot = Quaternion.LookRotation(worldPosition -
objectToMove.position, objectToMove.TransformDirection(Vector3.up));
float counter = 0;
while (counter < duration)
{
counter += Time.deltaTime;
objectToMove.rotation =
Quaternion.Lerp(currentRot, newRot, counter / duration);
yield return null;
}
}
IEnumerator lookatSmooth(Transform objectToMove、Vector3 worldPosition、浮点持续时间)
{
四元数currentRot=objectToMove.rotation;
四元数newRot=四元数。LookRotation(世界位置-
objectToMove.position,objectToMove.TransformDirection(Vector3.up));
浮点计数器=0;
while(计数器<持续时间)
{
计数器+=时间增量时间;
物体运动旋转=
四元数Lerp(currentRot、newRot、计数器/持续时间);
收益返回空;
}
}
您的新脚本:
public class helloTrigger : MonoBehaviour
{
public Animator anim;
public CharacterController player;
public Transform Player;
Coroutine smoothMove = null;
// Use this for initialization
void Start()
{
player = GameObject.FindObjectOfType<CharacterController>();
anim = GetComponent<Animator>();
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
anim.Play("Hello");
LookSmoothly();
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
anim.Play("Idle");
}
}
void LookSmoothly()
{
float time = 1f;
Vector3 lookAt = Player.position;
lookAt.y = transform.position.y;
//Start new look-at coroutine
if (smoothMove == null)
smoothMove = StartCoroutine(LookAtSmoothly(transform, lookAt, time));
else
{
//Stop old one then start new one
StopCoroutine(smoothMove);
smoothMove = StartCoroutine(LookAtSmoothly(transform, lookAt, time));
}
}
IEnumerator LookAtSmoothly(Transform objectToMove, Vector3 worldPosition, float duration)
{
Quaternion currentRot = objectToMove.rotation;
Quaternion newRot = Quaternion.LookRotation(worldPosition -
objectToMove.position, objectToMove.TransformDirection(Vector3.up));
float counter = 0;
while (counter < duration)
{
counter += Time.deltaTime;
objectToMove.rotation =
Quaternion.Lerp(currentRot, newRot, counter / duration);
yield return null;
}
}
}
公共类helloTrigger:MonoBehavior
{
公共动画;
公共人物控制器;
公共转换播放器;
协同程序smoothMove=null;
//用于初始化
void Start()
{
player=GameObject.FindObjectOfType();
anim=GetComponent();
}
无效对撞机(对撞机其他)
{
如果(other.tag==“玩家”)
{
动漫游戏(“你好”);
外观光滑();
}
}
void OnTriggerExit(碰撞器其他)
{
如果(other.tag==“玩家”)
{
动漫游戏(“空闲”);
}
}
void looksleep()
{
浮动时间=1f;
Vector3注视=玩家位置;
lookAt.y=transform.position.y;
//从新的角度看待协同程序
if(smoothMove==null)
smoothMove=start例程(lookatSmooth(变换、注视、时间));
其他的
{
//停止旧的,然后开始新的
StopCorroutine(smoothMove);
smoothMove=start例程(lookatSmooth(变换、注视、时间));
}
}
IEnumerator lookatSmooth(变换对象移动、矢量3世界位置、浮动持续时间)
{
四元数currentRot=objectToMove.rotation;
四元数newRot=四元数。LookRotation(世界位置-
objectToMove.position,objectToMove.TransformDirection(Vector3.up));
浮点计数器=0;
while(计数器<持续时间)
{
计数器+=时间增量时间;
物体运动旋转=
四元数Lerp(currentRot、newRot、计数器/持续时间);
收益返回空;
}
}
}
如果不打算使用更新
,请将其删除。Unity在编译时创建一个对象列表,如果定义了它,则调用Update
。因此,在不使用的情况下进行Update
,对性能有不良影响。顶级抽屉!!!帮助我更清楚地了解如何以及为什么使用协同程序。非常感谢。的确我一开始还不确定什么是合作项目!非常感谢。