[英]Unity - How can I use a flag to decide whether all the objects rotate at once or each one individually?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class SpinableObject
{
[SerializeField]
private Transform t;
[SerializeField]
private float rotationSpeed;
[SerializeField]
private float minSpeed;
[SerializeField]
private float maxSpeed;
[SerializeField]
private float speedRate;
private bool slowDown;
public void Rotate()
{
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
{
private bool slowDown = false;
private GameObject[] allPropellers;
public bool rotateAll = false;
public float rotationSpeed;
public float slowdownMax;
public float slowdownMin;
public SpinableObject[] objectsToRotate;
// Use this for initialization
void Start()
{
allPropellers = GameObject.FindGameObjectsWithTag("Propeller");
}
// Update is called once per frame
void Update()
{
if (rotateAll == false)
{
for (int i = 0; i < objectsToRotate.Length; i++)
{
objectsToRotate[i].Rotate();
}
}
else
{
objectsToRotate = new SpinableObject[allPropellers.Length];
for (int i = 0; i < allPropellers.Length; i++)
{
objectsToRotate[i].Rotate();
}
}
}
}
In the else in this part I want that all the objects will rotate with the global variables settings. 在本部分的其他部分,我希望所有对象都将随着全局变量设置一起旋转。 And if the rotateAll is false each one will rotate with their own options settings. 而且,如果rotateAll为false,则每个人都将使用自己的选项设置进行旋转。
objectsToRotate = new SpinableObject[allPropellers.Length];
for (int i = 0; i < allPropellers.Length; i++)
{
objectsToRotate[i].Rotate();
}
But here I'm only make instance for more places in the objectsToRotate they are all null. 但是在这里,我仅使objectToRotate中更多位置的实例全部为空。 And I'm not sure using objectsToRotate is good to rotate them all at once. 而且我不确定使用objectsToRotate是否可以一次旋转它们。
Update: This is what i tried now: 更新:这是我现在尝试的:
I changed the SpinableObject script to: 我将SpinableObject脚本更改为:
using System.Collections;
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 class SpinObject : MonoBehaviour
{
public SpinableObject[] objectsToRotate;
private Rotate _rotate;
private int index = 0;
private bool rotateAll;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
var _objecttorotate = objectsToRotate[index];
_rotate.t = _objecttorotate.t;
_rotate.rotationSpeed = _objecttorotate.rotationSpeed;
_rotate.minSpeed = _objecttorotate.minSpeed;
_rotate.maxSpeed = _objecttorotate.maxSpeed;
_rotate.speedRate = _objecttorotate.speedRate;
_rotate.slowDown = _objecttorotate.slowDown;
index++;
}
}
And created a new script name Rotate: 并创建了一个新的脚本名称Rotate:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotate : MonoBehaviour
{
public Transform t;
public float rotationSpeed;
public float minSpeed;
public float maxSpeed;
public float speedRate;
public bool slowDown;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
RotateObject();
}
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);
}
}
The idea is to feed the variables and settings in the script Rotate from the script SpinableObject. 这个想法是在脚本SpinableObject的脚本Rotate中提供变量和设置。
But i messed it up it's not working and give me some null exception. 但我搞砸了它无法正常工作,并给我一些空异常。
Is it a good way ? 这是个好方法吗? And how can i fix the scripts to work with each other so the SpinableObject will feed the Rotate with data. 我如何修复脚本以使其相互配合,以便SpinableObject将数据提供给Rotate。
This is how I would approach your problem. 这就是我要解决您的问题的方式。
I will define a global variable to determine if objects are rotating all at the same time or independently. 我将定义一个全局变量,以确定对象是同时旋转还是独立旋转。
Then I will create a script called rotation and I will add it to each GameObject in your scene which will rotate. 然后,我将创建一个名为rotation的脚本,并将其添加到场景中将旋转的每个GameObject中。
In this script I will just include the 在此脚本中,我将仅包含
...
Update()
{
if (rotateAll == false)
{
Rotate(allProperties)
}else{
Rotate(particularProperties)
}
}
//Here you can implement the Rotate function passing the attributes you consider to make the rotation different for each case
...
And about global variables in Unity. 关于Unity中的全局变量。 One possible approach is to define a class like: 一种可能的方法是定义一个类似以下的类:
public static class GlobalVariables{
public static boolean rotationType;
}
Then you can access and modigy this variable from another script like: 然后,您可以从另一个脚本访问和修改此变量,例如:
GlobalVariables.rotationType = true;
if(GlobalVariables.rotationType){
...
}
After Edit: 编辑后:
I can´t really tell what you are doing wrong, but if there is a null exception it may be you are not calling properly from the script the gameObjects you want to rotate. 我无法真正说出您在做什么错,但是如果存在null异常,则可能是您没有从脚本正确调用要旋转的gameObjects。 It could be you are not linking them correctly in the inspector. 可能是您在检查器中未正确链接它们。 If you have declare 如果您有申报
public SpinableObject[] objectsToRotate;
It is possible you forgot to drag and drop in the inspector the GamesObjets into the array to populate it. 您可能忘了将GamesObjets的检查器拖放到阵列中以进行填充。 But I can't be sure if the problem is just that. 但是我不能确定问题是否就在此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.