【问题标题】:Turn a text on y axis在 y 轴上翻转文本
【发布时间】:2018-02-22 16:11:45
【问题描述】:

我正在为学校创建一个项目,我必须制作一个小游戏。但我有一个问题:'(
我想创建一个代码(不是动画)来在 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);

【问题讨论】:

  • 请将您的代码发布为文本,而不是图像。
  • 请显示“反转”文本的屏幕截图。 “等于 90” 那是什么?

标签: c# user-interface unity3d


【解决方案1】:

Unity 使用Quaternions 表示旋转,应小心处理。

我建议你使用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 ;
    }
}

【讨论】:

  • 感谢您的回答!我刚刚对您的代码(pastebin.com/H1dMyws8)进行了一些更改,但我遇到了问题!文本再次反转...文本更改,但 1 的方向不正确(面向右侧插入左侧 >.
  • 再次,小心您使用的是 四元数y 组件,而不是 eulerAngles。此外,如果您想提高技能,应该考虑我建议的优化。
  • 感谢您的建议!但这并不能解决我的问题:/
  • 只是因为最终的旋转翻转了游戏对象(旋转 180°)。是否要将游戏对象旋转 360°?
【解决方案2】:

感谢 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";
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多