using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class SpinableObject
{
public Transform t;
public float rotationSpeed;
public float minSpeed;
public float maxSpeed;
public float speedRate;
public bool slowDown;
public void RotateObject()
{
if (rotationSpeed > maxSpeed)
slowDown = true;
else if (rotationSpeed < minSpeed)
slowDown = false;
rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
t.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
}
}
public class SpinObject : MonoBehaviour
{
[SerializeField]
[Header("Global Rotation")]
[Space(5)]
public float rotationSpeed;
public float minSpeed;
public float maxSpeed;
public float speedRate;
public bool slowDown;
public List<GameObject> allObjects;
[Space(5)]
[Header("Rotation Mode")]
[LabeledBool("Global Rotation", "Individual Rotation")]
[SerializeField]
bool _rotationMode = true;
[Header("Individual Rotation")]
[Space(3)]
public SpinableObject[] individualObjects;
private void Start()
{
allObjects = new List<GameObject>();
foreach(Transform t in transform)
{
}
}
// Update is called once per frame
void Update()
{
if (_rotationMode == false)
{
foreach (var spinner in individualObjects)
spinner.RotateObject();
}
else
{
for (int i = 0; i < allObjects.Count; i++)
{
RotateAllObjects(allObjects[i].transform);
}
}
}
private void RotateAllObjects(Transform t)
{
if (rotationSpeed > maxSpeed)
slowDown = true;
else if (rotationSpeed < minSpeed)
slowDown = false;
rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
t.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
}
}
開始我內心想的名字沒有標註「螺旋槳」 我有4級孩子的對象添加子對象:「Propeller1」,「Propeller2」,「Propeller3」,「Propeller4」如何按名稱查找所有特定的兒童gameobjects並將它們添加到列表中?
我想將這4個對象添加到allObjects中 因此,它只會旋轉該腳本附加到的這個對象的4個指定者。由於我克隆的對象更多,我不想使用FindByTag。