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();
}
}
}
}在此部分的else中,我希望所有对象都将使用全局变量设置进行旋转。如果rotateAll为false,则每一个都将使用自己的选项设置进行旋转。
objectsToRotate = new SpinableObject[allPropellers.Length];
for (int i = 0; i < allPropellers.Length; i++)
{
objectsToRotate[i].Rotate();
}但在这里,我只为objectsToRotate中的更多位置创建实例,它们都是空的。而且我不确定使用objectsToRotate一次旋转它们是否合适。
更新:这是我现在尝试的:
我将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++;
}
}并创建了一个新脚本,名称为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);
}
}其思想是从脚本编辑器( script SpinableObject )提供脚本旋转中的变量和设置。
但是我搞砸了,它不工作了,给了我一些空的异常。
这是一种好方法吗?我如何修复这些脚本,使它们相互协作,这样SpinableObject就会为旋转提供数据。
发布于 2017-07-23 17:31:16
这就是我处理你的问题的方法。
我将定义一个全局变量来确定对象是同时旋转还是独立旋转。
然后,我将创建一个名为rotation的脚本,并将其添加到场景中要旋转的每个GameObject中。
在这个脚本中,我将只包含
...
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
...关于Unity中的全局变量。一种可能的方法是定义一个类,如:
public static class GlobalVariables{
public static boolean rotationType;
}然后您可以从另一个脚本访问和修改此变量,如下所示:
GlobalVariables.rotationType = true;
if(GlobalVariables.rotationType){
...
}编辑后:
我真的不知道你做错了什么,但如果有一个空异常,那可能是你没有从脚本中正确地调用你想要旋转的gameObjects。这可能是因为您没有在检查器中正确链接它们。如果您已声明
public SpinableObject[] objectsToRotate;您可能忘记了在检查器中将GamesObjets拖放到数组中以填充它。但我不能确定问题是否就是这样。
https://stackoverflow.com/questions/45263440
复制相似问题