C# 如何重置变换。旋转位置
我希望能够在点击鼠标左键时,将对象旋转到(15,0,0),然后在大约一秒钟后将其旋转回(0,0,0) 问题是,它不转回来,我似乎无法让它回到C# 如何重置变换。旋转位置,c#,unity3d,C#,Unity3d,我希望能够在点击鼠标左键时,将对象旋转到(15,0,0),然后在大约一秒钟后将其旋转回(0,0,0) 问题是,它不转回来,我似乎无法让它回到 using UnityEngine; using System.Collections; public class HitScript : MonoBehaviour { private bool ifswung; // Use this for initialization void Start() {
using UnityEngine;
using System.Collections;
public class HitScript : MonoBehaviour
{
private bool ifswung;
// Use this for initialization
void Start()
{
ifswung = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (ifswung == false)
{
transform.Rotate(15, 0, 0);
ifswung = true;
}
else if(ifswung == true)
{
transform.Rotate(0, 0, 0);
ifswung = false;
}
}
}
}
所以在数组中存储了两个旋转。您也可以使用布尔值,使用数组使其超过两行
因此,每次按下按钮时,索引都会增加,如果索引高于数组的长度,则返回到0
最后一行是要从一个旋转缓慢移动到另一个旋转。您必须注释掉if语句中的一个。默认情况下,不需要构造函数。无需检查
bool
的两个值:如果它不是false
,则它是true
。您是否尝试过使用Rotate(-15,0,0)
向后旋转?非常有用。尽管对象旋转回-15的速度非常快,但这仍然有效。你知道在旋转到(-15,0,0)之前有什么方法让它暂停吗?你可以使用协程来实现这个目标。首先了解Invoke
。你知道,出于教学方面的原因,我认为最好让业余程序员使用transform.eulerAngles
和“永不提及”四元数。
public class HitScript : MonoBehaviour
{
private Vector3[] rotations = { new Vextor3(15f,0f,0f),Vector3.zero } ;
private in index = 0;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if(++index >= rotations.Length)index = 0;
transform.rotation = Quaternion.Euler(rotations[index]);
}
}
//transform.rotation = Quaternion.Lerp(transform.rotation,
// Quaternion.Euler(rotations[index]), Time.deltaTime);
}