简体   繁体   中英

How can I decide when to decrease the rotation speed and when the increase depending on the speed reached value?

if(objectsToRotate[i].tag == "RotateAutomatic")
            {
                if (a_Speed == 100)
                    a_Speed -= 1;

                a_Speed += 1;

                objectsToRotate[i].transform.Rotate(new Vector3(0, 1, 0) * Time.deltaTime * a_Speed, Space.World);
            }

I want that first time when running the game the objects speed will increase to 100 once the speed is 100 decrease the speed to 0 once it's 0 again increase to 100 and so on between 100 and 0.

Least code solution, based on the code that I can see, should be to simply introduce a boolean trigger which you toggle when reaching certain values:

private bool _shouldIncrease = true;

YourFunction() {
    if (objectsToRotate[i].tag == "RotateAutomatic")
    {
        if (a_Speed >= 100)
            _shouldIncrease = false;

        if (a_Speed <= 1)
            _shouldIncrease = true;

        a_Speed += _shouldIncrease ? 1 : -1;

        objectsToRotate[i].transform.Rotate(new Vector3(0, 1, 0) * Time.deltaTime * a_Speed, Space.World);
    }
}
using UnityEngine;
public class RotatesCollection : MonoBehaviour
{

    [SerializeField] Transform[] _objectsToRotateManual;
    [SerializeField] Transform[] _objectsToRotateAutomatic;
    int _value = 0;
    int _valueDelta = 1;

    void Update ()
    {
        float deltaTime = Time.deltaTime;
        foreach( Transform rotor in _objectsToRotateAutomatic )
        {
            //step:
            _value += _valueDelta;

            //reverse step direction when min/max value is met:
            if( _value==0 || _value==100 ) { _valueDelta *= -1; }

            //rotate:
            rotor.Rotate(
                Vector3.up ,
                deltaTime * _value ,
                Space.World
            );
        }
    }

}

You can use animation curves for that too. This way speed control will be easier to change and tweak:

动画曲线检查器

using UnityEngine;
public class RotatesCollection : MonoBehaviour
{

    [SerializeField] AnimationCurve _speedAnimation = AnimationCurve.Linear( 0f , 0f , 4f , 100f );

    [SerializeField] Transform[] _objectsToRotate;

    float _timeEnabled;

    void OnEnable ()
    {
        //get time when enabled:
        _timeEnabled = Time.time;

        //change default wrap mode to loop:
        if( _speedAnimation.postWrapMode == WrapMode.Clamp )
        {
            _speedAnimation.postWrapMode = WrapMode.Loop;
        }
    }

    void Update ()
    {
        float deltaTime = Time.deltaTime;
        float timeSinceEnabled = Time.time - _timeEnabled;
        foreach( Transform rotor in _objectsToRotate )
        {
            //get speed from curve:
            float speed = _speedAnimation.Evaluate( timeSinceEnabled );

            //rotate:
            rotor.Rotate(
                Vector3.up ,
                deltaTime * speed ,
                Space.World
            );
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM