Daniel Lip
Daniel Lip

Reputation: 11341

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.

Upvotes: 0

Views: 110

Answers (3)

Andrew Łukasik
Andrew Łukasik

Reputation: 1637

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

AnimationCurve inspector

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
            );
        }
    }

}

Upvotes: 1

Andrew Łukasik
Andrew Łukasik

Reputation: 1637

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
            );
        }
    }

}

Upvotes: 1

Fredrik
Fredrik

Reputation: 5108

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);
    }
}

Upvotes: 1

Related Questions