我应该在更新中添加什么以在运行时应用lookAtSpecificTarget标志和lookAtSpecific Targ


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
public FLookAnimator lookAnimator;
public Transform[] targets;
public bool lookAtSpecificTarget = false;
public int lookAtSpecificTargetIndex;
public float switchingTime;
public bool switchingLoop = false;
private bool loopOnce = true;
// Start is called before the first frame update
private void Start()
{
if (lookAtSpecificTarget && targets.Length > 0)
{
if (lookAtSpecificTargetIndex >= 0)
{
lookAnimator.ObjectToFollow = targets[lookAtSpecificTargetIndex];
}
}
else
{
StartCoroutine(SwitchTargetsPeriodically());
}
}
private void Update()
{
}
IEnumerator SwitchTargetsPeriodically()
{
for (int i = 0; true; i = (i + 1) % targets.Length)
{
if (i <= targets.Length - 1 && switchingLoop)
{
loopOnce = true;
lookAnimator.ObjectToFollow = targets[i];
}
if (switchingLoop == false && loopOnce)
{
if (i <= targets.Length - 1)
{
lookAnimator.ObjectToFollow = targets[i];
}
if (i == (targets.Length - 1))
{
loopOnce = false;
}
}
yield return new WaitForSeconds(switchingTime);
}
}
}

第一次运行游戏时运行良好,但我应该在更新中做什么来在运行时应用更新中目标的标志和索引的更改?

目标是能够切换目标索引,以便在标志为true的情况下查看运行时。如果标志为false,请不要使用lookAtSpecificTargetIndex并继续使用Coroutine,但如果标志设置为true,则暂停Coroutine并使用lookAtSpecificTargetIndex:

lookAnimator.ObjectToFollow = targets[lookAtSpecificTargetIndex];

如果您正在为编辑器寻找一种快速的方法,您可以使用类似的[ContextMenu]

private Coroutine _routine;
[ContextMenu(nameof(ApplyTargetIndex))]
private void ApplyLookAtTarget()
{
if (lookAtSpecificTarget && targets.Length > 0)
{
if(_routine!= null) StopCoroutine(_routine);
_routine = null;
if (lookAtSpecificTargetIndex >= 0)
{
lookAnimator.ObjectToFollow = targets[lookAtSpecificTargetIndex];
}
}
else
{
_routine = StartCoroutine(SwitchTargetsPeriodically());
}
}

这允许您通过组件的检查器的上下文菜单执行此方法

相关内容

  • 没有找到相关文章

最新更新