Help in calculating rotation angle

I need a little bit of help in calculating the angle I need to rotate my cube.

So, I have a cube that I want to rotate in a circle around a certain point. I did this using the following code :

void Update()
    {
        angle = angle + Time.deltaTime * RotateSpeed;
        if (angle >= 360f)
            angle = 0;
        float posX = center.x + Mathf.Cos(angle) * Radius;
        float posZ = center.z + Mathf.Sin(angle) * Radius;
        transform.position = new Vector3(posX, transform.position.y, posZ);

    }

Now, I want to at the same time rotate the cube such that it is always facing the center(The camera at the center of the cube always sees the same face of the cube). How do I go about doing this?

Isn’t that exactly what transform.LookAt() is for? Have an object on the center (let’s call it ‘faceme’, in your case it’s the camera itself) and put that object into a public GameObject faceObject; in your cube’s script. Now, in the cube’s Update(),

gameObject.transform.LookAt(faceObject);

and you are done.

Normal rotations look like transform.rotation=Quatenion.Euler(0,angle,0);. It uses “unity coords”, which is degrees, clockwise, where 0 is along +z. But LookAt is often an easy shortcut.

For the movement, trig is the hard way, plus it uses radians and goes backwards from Unity. Standard 3D built-ins take a forward arrow and spin it by the angle:
transform.position=center+Quaternion.Euler(0,angle,0)(Vector3.forwardRadius);. This way is also simpler to adjust for other axis.

Hey, thanks. This worked. I didn’t know of the LookAt function, but it worked perfectly :slight_smile: