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);
}
我希望第一次运行游戏时,一旦速度为100,对象的速度将增加到100;一旦速度为0,则将速度减小到0;再次增加到100,以此类推,在100到0之间。
答案 0 :(得分:1)
基于我所看到的代码,最少的代码解决方案应该是简单地引入一个布尔触发器,当达到某些值时可以进行切换:
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);
}
}
答案 1 :(得分:1)
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
);
}
}
}
答案 2 :(得分:1)
您也可以使用动画曲线。这样,速度控制将更易于更改和调整:
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
);
}
}
}