简体   繁体   English

在Unity中旋转我已经移动的对象

[英]Rotate my already moving object in Unity

I am learning to code by making games in unity. 我正在学习通过统一制作游戏来编写代码。 I have a rock which is moving up and down and cannot get it to rotate from the center. 我有一块正在上下移动的岩石,无法使其从中心旋转。 I tried looking for a solution but could not find it. 我试图寻找解决方案,但找不到。 any ideas? 有任何想法吗?

IEnumerator Move(Vector3 target)
{
    while (Mathf.Abs((target - transform.localPosition).y) > 0.20f)
    {
        Vector3 direction = target.y == topPosition.y ? Vector3.up : Vector3.down;
        transform.localPosition += direction * Time.deltaTime * speed;

        yield return null;
    }

    yield return new WaitForSeconds (0.5f);

    Vector3 newTarget = target.y == topPosition.y ? bottomPosition : topPosition;

    StartCoroutine (Move (newTarget));
}

Can you set a new eulerAngles into your object? 您可以在对象中设置新的eulerAngles吗? Maybe you can create a new based on your transform.eulerAngles. 也许您可以基于您的transform.eulerAngles创建一个新的。

Vector3 newAngles = new Vector3(transform.eulerAngles.x-1, transform.eulerAngles.y, transform.eulerAngles.z);
transform.eulerAngles = newAngles;

in this example i "apllied" a rotation to the X axis. 在此示例中,我“允许”旋转到X轴。 Maybe you can do something like this in your Update method. 也许您可以在Update方法中执行类似的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM