C# 在y轴上旋转文本
我正在为学校做一个项目,我要做一个小游戏。但我有个问题:' 我想创建一个代码,而不是一个动画,使文字在y轴上旋转。我的代码在帖子的末尾 轮到它了,但动画结束时,文本反转: 有什么解决办法吗? 同样,当y轴上的旋转为egal到90时,我想更改文本。我知道怎么做,但那只是一个精度:pC# 在y轴上旋转文本,c#,user-interface,unity3d,C#,User Interface,Unity3d,我正在为学校做一个项目,我要做一个小游戏。但我有个问题:' 我想创建一个代码,而不是一个动画,使文字在y轴上旋转。我的代码在帖子的末尾 轮到它了,但动画结束时,文本反转: 有什么解决办法吗? 同样,当y轴上的旋转为egal到90时,我想更改文本。我知道怎么做,但那只是一个精度:p transform.GetChild(0).transform.rotation = Quaternion.Lerp(transform.GetChild(0).transform.rotation, new Quat
transform.GetChild(0).transform.rotation = Quaternion.Lerp(transform.GetChild(0).transform.rotation, new Quaternion(0, 180, 0, 0), 0.01f);
Unity用于表示旋转,应小心处理
我建议你使用四元数。欧拉,它以简单的角度作为参数
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 ;
}
}
谢谢Hellium的帮助,但我找到了问题的解决方案!如果您有时间,请尝试优化此代码,我不是Unity的专业人士:/
Quaternion fromRotation = transform.GetChild(0).rotation;
Quaternion toRotation = Quaternion.Euler(0, 180, 0);
Quaternion rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime * 3f);
child.rotation = rotation;
if (child.GetComponent<Text>().text == name && child.rotation.y < -0.6)
{
child.localScale = new Vector3(-1, child.localScale.y, child.localScale.z);
child.GetComponent<Text>().text = "1";
}
请以文本而不是图像的形式发布代码。请显示反转文本的屏幕截图。egal to 90那是什么?谢谢你的回答!我刚刚对你的代码做了一些更改,但我有一个问题!课文又颠倒了。。。文本会发生变化,但1的方向不是朝右而是朝左>。请再次注意,您使用的是四元数的y分量,而不是eulerAngles。此外,如果你想提高你的技能,你应该考虑我所建议的优化。谢谢你的建议!但这并不能解决我的问题:/仅仅因为最终旋转翻转了游戏对象180度的旋转。是否要将游戏对象旋转360°?