using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
public class SpreadedFireDistanceCheck : MonoBehaviour
{
public Transform player;
public List<GameObject> spreadedFires = new List<GameObject> ();
private Animator anim;
private bool doOnce = true;
void Start()
{
spreadedFires = GameObject.FindGameObjectsWithTag("Spreaded Fire").ToList();
anim = player.GetComponent<Animator>();
}
private void Update()
{
foreach(GameObject spreadedFire in spreadedFires)
{
if(Vector3.Distance(player.transform.position, spreadedFire.transform.position) < 5f && doOnce)
{
anim.Play("Walk Backward");
doOnce = false;
}
else
{
doOnce = true;
}
}
}
}
有 8 個蔓延的火焰物件,我想在玩家接近其中一個蔓延的火焰時播放一次影片。
問題是我需要制作一個回圈,這樣它會播放影片 8 次,而不是只播放一次,具體取決于玩家離其中一團蔓延的火焰有多近。如果范圍內有兩個或多個蔓延的火焰物件,也會播放一次影片。
uj5u.com熱心網友回復:
如果您想要的是if any of your spreadedFire is close enough, then play the fire animation (only) once in each frame,那么您可以使用LinQ Any Operator:
using System.Linq;
......
private void Update()
{
if (spreadedFires.Any(x => IsCloseEnough(x)))
{
anim.Play("Walk Backward");
}
}
private bool IsCloseEnough(GameObject spreadedFire)
{
return Vector3.Distance(player.transform.position, spreadedFire.transform.position) < 5f;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/533585.html
標籤:C#unity3d
上一篇:第四章 linux字符設備驅動一
