C# 随时间以速度旋转和缩放对象
我正在尝试缩放和旋转一个对象。旋转是在X轴上进行的。我能够完美地实现刻度,这是旋转造成的问题。如何使用此脚本将对象旋转到目标旋转,然后再返回到初始旋转?当我执行这个脚本时,旋转有时工作,有时不执行,但是缩放没有问题 编辑:我发现的基本情况是,我不能将四元数设置为负值,即-90自动变为270,因此我不能将270移动到0C# 随时间以速度旋转和缩放对象,c#,unity3d,C#,Unity3d,我正在尝试缩放和旋转一个对象。旋转是在X轴上进行的。我能够完美地实现刻度,这是旋转造成的问题。如何使用此脚本将对象旋转到目标旋转,然后再返回到初始旋转?当我执行这个脚本时,旋转有时工作,有时不执行,但是缩放没有问题 编辑:我发现的基本情况是,我不能将四元数设置为负值,即-90自动变为270,因此我不能将270移动到0 using UnityEngine; using System.Collections; public class ScaleAndRotate : MonoBehaviou
using UnityEngine;
using System.Collections;
public class ScaleAndRotate : MonoBehaviour {
public int startSize = 3;
public int minSize = 1;
public int maxSize = 6;
public float speed = 2.0f;
private Vector3 targetScale;
private Vector3 baseScale;
private int currScale;
//ROT
public Quaternion targetRotation;
public bool startRotation = false;
void Start() {
baseScale = transform.localScale;
transform.localScale = baseScale * startSize;
currScale = startSize;
targetScale = baseScale * startSize;
}
void Update() {
transform.localScale = Vector3.Lerp (transform.localScale, targetScale, speed * Time.deltaTime);
if(startRotation == true)
{
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * speed);
}
else
if(startRotation == false)
{
//Go Back To Initial Rotation
}
if (Input.GetKeyDown (KeyCode.UpArrow))
{
ChangeSize (true);
startRotation = true;
}
if (Input.GetKeyDown (KeyCode.DownArrow))
{
ChangeSize (false);
startRotation = false;
}
}
public void ChangeSize(bool bigger) {
if (bigger)
{
currScale++;
}
else
{
currScale--;
}
currScale = Mathf.Clamp (currScale, minSize, maxSize+1);
targetScale = baseScale * currScale;
}
}
我认为问题在于,当您使用if语句时,会遗漏括号:
if (Input.GetKeyDown (KeyCode.UpArrow))
ChangeSize (true);
startRotation = true;
if (Input.GetKeyDown (KeyCode.DownArrow))
ChangeSize (false);
startRotation = false;
当您使用类似上面的代码时,if仅用于下面的第一行代码。因此,在该代码中添加括号:
if (Input.GetKeyDown (KeyCode.UpArrow))
{
ChangeSize (true);
startRotation = true;
}
if (Input.GetKeyDown (KeyCode.DownArrow))
{
ChangeSize (false);
startRotation = false;
}
问题在于transform.rotation无法将旋转从-90/270更改为新旋转。相反,我不得不使用transform.localRotation,它工作得非常好
using UnityEngine;
using System.Collections;
public class ScaleAndRotate : MonoBehaviour {
public int startSize = 3;
public int minSize = 1;
public int maxSize = 6;
public float speed = 2.0f;
private Vector3 targetScale;
private Vector3 baseScale;
private int currScale;
//ROT
public Quaternion targetRotation;
public Quaternion initialRotation;
public bool startRotation = false;
void Start() {
baseScale = transform.localScale;
transform.localScale = baseScale * startSize;
currScale = startSize;
targetScale = baseScale * startSize;
initialRotation = transform.localRotation;
}
void Update() {
transform.localScale = Vector3.Lerp (transform.localScale, targetScale, speed * Time.deltaTime);
if(startRotation == true)
{
transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, Time.deltaTime * speed);
}
else
if(startRotation == false)
{
//Go Back To Initial Rotation
transform.localRotation = Quaternion.Slerp(transform.localRotation, initialRotation, Time.deltaTime * speed);
}
if (Input.GetKeyDown (KeyCode.UpArrow))
{
ChangeSize (true);
startRotation = false;
}
if (Input.GetKeyDown (KeyCode.DownArrow))
{
ChangeSize (false);
startRotation = true;
}
}
public void ChangeSize(bool bigger) {
if (bigger)
{
currScale++;
}
else
{
currScale--;
}
currScale = Mathf.Clamp (currScale, minSize, maxSize+1);
targetScale = baseScale * currScale;
}
}
嘿,谢谢!我后来发现,并在代码中对其进行了更改,旋转仍然不起作用。也就是说,如果旋转为270,并且我希望它更改为0,则它不会旋转,但会从0旋转到270。是否确实正确设置了该公共目标四元数?也许将其更改为公共矢量3目标,使其现在处于欧拉角。然后,您可以使用Quaternion.eulerTargetRotation将其转换为四元数问题是我不能将其更改为-90,它会自动更改为270。从270到0,它不知何故永远不会运行。你能演示一下如何初始化公共四元数targetRotation;?“检查器”窗口中的公共四元数targetRotation=270,0,0,因为-90自动更改为270。我无法将四元数设置为负值,即-90自动变为270,因此我无法将270移动到0。。。四元数有四个分量x,y,z,w,它们都在-1和1之间移动。。。所以现在还不清楚你在说什么。。。您是通过Inspector设置值的吗?是的,我是通过Inspector设置值的。@derHugo是否有其他解决方案?我在等待你的建议。如果你的主要问题只是轮换和本地轮换,那么这应该已经是答案了。。。使用基于Time.deltaTime的因子的Lerp毫无意义。。。如果你从当前旋转开始,朝着一个目标进行逐帧旋转,你要么想要一个固定因子,例如0.5f,要么使用一些东西,谢谢,我会尝试一下。