using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpinObject : MonoBehaviour
{
public float rotationMultiplier;
public GameObject[] objectsToRotate;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
for (int i = 0; i < objectsToRotate.Length; i++)
{
rotationMultiplier += 0.1f;
objectsToRotate[i].transform.Rotate(Vector3.forward, Time.deltaTime * rotationMultiplier);
}
}
}
通过这条线,我可以慢慢加速旋转:
rotationMultiplier += 0.1f;
现在我想添加一个IF条件,所以如果rotationMultiplier达到例如速度500,那么开始减速如下:
rotationMultiplier - = 0.1f;
问题是rotationMultiplier是一个浮点数所以我不能只检查IF rotationMultiplier == 500
答案 0 :(得分:1)
添加布尔值以检查是否必须加速或减速
private bool slowDown = false;
for (int i = 0; i < objectsToRotate.Length; i++)
{
if( rotationMultiplier > 500)
slowDown = true ;
else if( rotationMultiplier < 0 )
slowDown = false;
rotationMultiplier = (slowDown) ? rotationMultiplier - 0.1f : rotationMultiplier + 0.1f;
objectsToRotate[i].transform.Rotate(Vector3.forward, Time.deltaTime * rotationMultiplier);
}
否则,您可以使用Mathf.PingPong
:
for (int i = 0; i < objectsToRotate.Length; i++)
{
rotationMultiplier = Mathf.PingPong( Time.time, 500 ) ;
objectsToRotate[i].transform.Rotate(Vector3.forward, Time.deltaTime * rotationMultiplier);
}
答案 1 :(得分:1)
您可以使用bool来确定您的状态(加速或减速)
public bool isIncreasing;
if(rotationMultiplier >= 500)
{
isIncreasing=false;
}
if(rotationMultiplier <= 0) //or your desired value
{
isIncreasing=true;
}
if(isIncreasing)
{
//do your speed up here
}
else
{
//do your slow down here
}
答案 2 :(得分:0)
您可以将float转换为int然后可以检查
for (int i = 0; i < objectsToRotate.Length; i++)
{
if(rotationMultiplier >= 500)
{
rotationMultiplier -= 0.1f;
}
else
{
rotationMultiplier += 0.1f;
}
objectsToRotate[i].transform.Rotate(Vector3.forward, Time.deltaTime * rotationMultiplier);
}