在某些地方,物体旋转速度变慢甚至非常慢,然后当我移动到其他地方时,物体旋转速度恢复到正确的速度 .
剧本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeScale : MonoBehaviour
{
//Scaling change
public GameObject objectToScale;
public float duration = 1f;
public Vector3 minSize;
public Vector3 maxSize;
private bool scaleUp = false;
private Coroutine scaleCoroutine;
//Colors change
public Color startColor;
public Color endColor;
public float colorDuration; // duration in seconds
//Lights change
public DimLights dimlights;
private Coroutine lightCoroutine;
//Rotate change
public float rotationSpeed;
public float rotationDuration;
private float x;
private void Start()
{
//startColor = GetComponent<Renderer>().material.color;
endColor = Color.green;
objectToScale.transform.localScale = minSize;
}
// Use this for initialization
void Update()
{
//Scaling
if (Input.GetKeyDown(KeyCode.F))
{
//Flip the scale direction when F key is pressed
scaleUp = !scaleUp;
//Stop old coroutine
if (scaleCoroutine != null)
StopCoroutine(scaleCoroutine);
if (lightCoroutine != null)
StopCoroutine(lightCoroutine);
//Scale up
if (scaleUp)
{
//Start new coroutine and scale up within 5 seconds and return the coroutine reference
scaleCoroutine = StartCoroutine(scaleOverTime(objectToScale, maxSize, duration));
lightCoroutine = StartCoroutine(dimlights.dimLightOverTime(1, duration)); ;
}
//Scale Down
else
{
//Start new coroutine and scale up within 5 seconds and return the coroutine reference
scaleCoroutine = StartCoroutine(scaleOverTime(objectToScale, minSize, duration));
lightCoroutine = StartCoroutine(dimlights.dimLightOverTime(0, duration)); ;
}
}
//Change color
if (Input.GetKeyDown(KeyCode.C))
{
StartCoroutine(ChangeColor());
}
x += Time.deltaTime * 1000;
objectToScale.transform.rotation = Quaternion.Euler(0, 0, x);
}
IEnumerator scaleOverTime(GameObject targetObj, Vector3 toScale, float duration)
{
float counter = 0;
//Get the current scale of the object to be scaled
Vector3 startScaleSize = targetObj.transform.localScale;
while (counter < duration)
{
counter += Time.deltaTime;
targetObj.transform.localScale = Vector3.Lerp(startScaleSize, toScale, counter / duration);
yield return null;
}
}
IEnumerator ChangeColor()
{
float t = 0;
while (t < colorDuration)
{
t += Time.deltaTime;
GetComponent<Renderer>().material.color = Color.Lerp(startColor, endColor, t / colorDuration);
yield return null;
}
}
}
这是我用于轮换的Update中的两行:
x += Time.deltaTime * 1000;
objectToScale.transform.rotation = Quaternion.Euler(0, 0, x);
该脚本附加到MainCamera,MainCamera是RigidBodyFPSController的子项(1)
我正在旋转的对象是NAVI,而NAVI是MainCamera的孩子 .
当我移动相机并且NAVI在某些区域以恒定速度旋转时,似乎旋转速度变慢并且在其他地方旋转速度快速且平滑 .
我不确定为什么它变得越来越慢,例如当我在门附近移动物体时 .
这是我录制的一个小视频片段,显示了我的意思:
这是一个截图,其中对象位于门前并且旋转缓慢:
但是当我将相机移动到这个位置时,物体会快速平滑地旋转,因为它应该是: