0
votes

I'm trying to do a simple thing in unity: rotate an object around an axis. But I'm missing something, my object just goes in the downward direction, instead of rotating around the axis.

This is my update function:

this.transform.RotateAround(new Vector3(1,0,5), new Vector3(0,1,0), 10 * Time.deltaTime);

where (1,0,5) is the center of rotation. My object is at position (0,0,0). The object just moves down, instead of rotating. Any idea why this is happening?

1
Maybe add a gif or a link to video of what's happening. - Programmer
This line of code looks correct. Are you sure you don't have another line or another script that moves your object. Does it stop moving when commenting this line? - Basile Perrenoud
Other possible problem is if the transform has a child object that has a different position and OP actually wants to rotate that object, not its parent. - Hristo
I think RotateAround works with world coordinates so if your GameObject is far from that (1,0,5) it will move on a big circle - Jonas Grumann

1 Answers

1
votes

I think it can solve your problem. This is the script you need:

using UnityEngine;

public class RotateObj : MonoBehaviour
{
    private void Update()
    {
        // rotate to its own axis
        transform.Rotate(new Vector3(Random.value, Random.value, Random.value)); 

        // rotate about axis passing through the point in world coordinates
        transform.RotateAround(Vector3.zero, Vector3.up, 1.0f); 
    }
}

and this is your unity configuration:

enter image description here

And it rotates around itself (randomly) and Vector3.zero coordinates

enter image description here