简体   繁体   English

Unity-如何使用标记确定所有对象是一次旋转还是单独旋转?

[英]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.

相关问题 我应该如何以及在代码中的何处使用 loop bool 标志来决定是循环还是在航路点之间进行一次移动? - How and where in the code should I use the loop bool flag to decide if to loop or to make once movement between the waypoints? 如何使用 bool 标志来决定是否在 MyComparer 类中使用反向? - How can I use a bool flag to decide if to use reverse or not in the MyComparer class? 如何决定是使用接口还是抽象类? - How do I decide whether to use an interface or an abstract class? 如何在更新中每次使用 foreach 循环一次,并且每次都使用 bool 标志? - How can I loop using foreach each time once in the Update and also using a bool flag each time once? 如何确定列表中的每个对象出现一次? - How can i be sure that each of the objects in the List appears once? Unity:如何旋转这些生成的障碍物? - Unity: How can I rotate these spawned obstacles? cursor 可以统一移动和旋转物体吗? - Can I move and rotate objects smoothly with the cursor in unity? 如何决定在类中使用属​​性还是方法 - How to decide whether property or method to use in a class 如何注入 IEnumerable<icustomrepository> 在构造函数中,所以我可以决定在基础 class 中使用哪一个?</icustomrepository> - How to inject a IEnumerable<ICustomRepository> in constructor, so I can decide which one to use in the base class? 使用和等待使用有什么区别? 我如何决定使用哪一个? - What is the difference between using and await using? And how can I decide which one to use?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM