简体   繁体   中英

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? Maybe you can create a new based on your 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. Maybe you can do something like this in your Update method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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