我正在为学校创建一个项目,我必须做一个小游戏。但我有一个问题:'(
我想创建一个代码(而不是动画)来在y轴上转动文本。我的代码在帖子的最后
当动画结束时,它完全 BUT ,文本反转:(
有没有解决方案?
也就是说,当y轴上的旋转为90°时,我想改变文本。我知道该怎么做但只是一个精度:p
transform.GetChild(0).transform.rotation = Quaternion.Lerp(transform.GetChild(0).transform.rotation, new Quaternion(0, 180, 0, 0), 0.01f);
答案 0 :(得分:1)
Unity使用Quaternions来表示轮换,应使用 care 进行处理。
我建议你使用Quaternion.Euler
,它将简单的角度作为参数。
public float rotationSpeed = 0.01f ;
private Transform child;
private float initialYRotation;
private bool textChanged = false;
private void Awake()
{
child = transform.GetChild(0);
initialYRotation = child.rotation.eulerAngles.y ;
}
private void Update()
{
Quaternion fromRotation = child.rotation ;
Quaternion toRotation = Quaternion.Euler( 0, 180, 0 ) ; // Not the same as new Quaternion(0, 180, 0, 0) !!!
Quaternion rotation = Quaternion.Lerp( fromRotation, toRotation, Time.deltaTime * rotationSpeed );
child.rotation = rotation;
if( !textChanged && Mathf.Abs( child.rotation.eulerAngles.y - initialYRotation ) > 90 )
{
// Change the text
textChanged = true ;
}
}
如果您希望物体完全旋转360°。四元数不理想,我认为你可以毫无问题地使用Vector3
:
public float rotationSpeed = 0.01f ;
private Transform child;
private float initialYRotation;
private bool textChanged = false;
private void Awake()
{
child = transform.GetChild(0);
initialYRotation = child.rotation.eulerAngles.y ;
}
private void Update()
{
Vector3 fromRotation = child.eulerAngles ;
Vector3 toRotation = new Vector3( 0, 360, 0 ) ;
Vector3 rotation = Vector3.Lerp( fromRotation, toRotation, Time.deltaTime * rotationSpeed );
child.eulerAngles = rotation;
if( !textChanged && Mathf.Abs( rotation.y - initialYRotation ) > 90 )
{
transform.GetChild(0).GetComponent<UnityEngine.UI.Text>().text = "Success";
textChanged = true ;
}
}