這部分無論是在編輯模式還是運行時都會在控制臺中不停地顯示 debug.log。
private void Update()
{
if (Physics.CheckSphere(transform.position, xRadius, targetLayers))
{
Debug.Log("player detected");
}
else
{
Debug.Log("player NOT detected");
}
}
我希望如果檢測到播放器顯示“檢測到播放器”一次,則播放器已退出并且未檢測到顯示其他文本一次,直到播放器再次進入并被檢測到,然后再次顯示檢測到的內容。
每次顯示一次,而不是馬不停蹄。問題是它在更新中。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[ExecuteAlways]
[RequireComponent(typeof(UnityEngine.LineRenderer))]
public class DrawCircle : MonoBehaviour
{
[Range(1, 50)] public int segments = 50;
[Range(1, 500)] public float xRadius = 5;
[Range(1, 500)] public float yRadius = 5;
[Range(0.1f, 5)] public float width = 0.1f;
[Range(0, 100)] public float height = 0;
public bool controlBothXradiusYradius = false;
public bool draw = true;
[SerializeField] private LayerMask targetLayers;
[SerializeField] private LineRenderer line;
private void Start()
{
if (!line) line = GetComponent<LineRenderer>();
if (draw)
CreatePoints();
}
private void Update()
{
if (Physics.CheckSphere(transform.position, xRadius, targetLayers))
{
Debug.Log("player detected");
}
else
{
Debug.Log("player NOT detected");
}
}
public void CreatePoints()
{
line.enabled = true;
line.widthMultiplier = width;
line.useWorldSpace = false;
line.widthMultiplier = width;
line.positionCount = segments 1;
float x;
float y;
var angle = 20f;
var points = new Vector3[segments 1];
for (int i = 0; i < segments 1; i )
{
x = Mathf.Sin(Mathf.Deg2Rad * angle) * xRadius;
y = Mathf.Cos(Mathf.Deg2Rad * angle) * yRadius;
points[i] = new Vector3(x, height, y);
angle = (380f / segments);
}
// it's way more efficient to do this in one go!
line.SetPositions(points);
}
#if UNITY_EDITOR
private float prevXRadius, prevYRadius;
private int prevSegments;
private float prevWidth;
private float prevHeight;
private void OnValidate()
{
// Can't set up our line if the user hasn't connected it yet.
if (!line) line = GetComponent<LineRenderer>();
if (!line) return;
if (!draw)
{
// instead simply disable the component
line.enabled = false;
}
else
{
// Otherwise re-enable the component
// This will simply re-use the previously created points
line.enabled = true;
if (xRadius != prevXRadius || yRadius != prevYRadius || segments != prevSegments || width != prevWidth || height != prevHeight)
{
CreatePoints();
// Cache our most recently used values.
prevXRadius = xRadius;
prevYRadius = yRadius;
prevSegments = segments;
prevWidth = width;
prevHeight = height;
}
if (controlBothXradiusYradius)
{
yRadius = xRadius;
CreatePoints();
}
}
}
#endif
}
uj5u.com熱心網友回復:
在這種情況下,我將使用一種迷你狀態機(將來易于擴展并且看起來不錯)。雖然你真的只需要一個成員變數來跟蹤狀態。
private enum states {
inSphere,
outSphere
}
states state = states.outSphere;
private void Update() {
switch(state) {
case states.outSphere:
if (Physics.CheckSphere(transform.position, xRadius, targetLayers)) {
Debug.Log("player detected");
state = states.inSphere;
}
break;
case states.inSphere:
if (!Physics.CheckSphere(transform.position, xRadius, targetLayers)) {
Debug.Log("player NOT detected");
state = states.outSphere;
}
break;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/472746.html