我怎样才能等到物体的旋转速度达到某个值然后减慢速度?
How can i wait until the rotation speed of a object is getting to some value then to slow down?
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 然后开始减速,如:
旋转倍数 -= 0.1f;
问题是 rotationMultiplier 是一个浮点数,所以我不能只检查 IF rotationMultiplier == 500
您可以将 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);
}
添加一个布尔值来检查是否必须加速或减速
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);
}
您可以使用布尔值来确定您的状态(加速或减速)
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
}
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 然后开始减速,如:
旋转倍数 -= 0.1f;
问题是 rotationMultiplier 是一个浮点数,所以我不能只检查 IF rotationMultiplier == 500
您可以将 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);
}
添加一个布尔值来检查是否必须加速或减速
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);
}
您可以使用布尔值来确定您的状态(加速或减速)
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
}