Hey guys, I got a little problem.

I have an enemy gameobject that I want to rotate around a background gameobject on a specific angle
I tried this bit of code

using UnityEngine;
using System.Collections;

public class rotatearoundme : MonoBehaviour {
	public float rotationSpeed = 5f;
	
	private float currentYAngle = 0f;
	
	private float targetYAngle = 0f;
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update() {
		if (Input.GetKey(KeyCode.Alpha1))
			targetYAngle = 0 - 90;
		else if (Input.GetKey(KeyCode.Alpha2))
			targetYAngle = 90 - 90;
		else if (Input.GetKey(KeyCode.Alpha3))
			targetYAngle = 180 - 90;
		else if (Input.GetKey(KeyCode.Alpha4))
			targetYAngle = 270 - 90;
		  
		currentYAngle = Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetYAngle, rotationSpeed * Time.deltaTime);
		transform.RotateAround(new Vector3(0f,6.73f,30f), Vector3.up, currentYAngle);
		 	}
}

But it doesn’t stop the rotation. The enemy just keep orbiting around the cube instead of stopping.
What did I miss?

A couple notes first:

Firstly, you cannot use transform.eulerAngles.y as continuous input in place of currentYAngle because of gimbal lock. At certain angles, the eulerAngles can snap/change, while currentYAngle remains as it was. You can search for this “issue” around. Generally, if you rotate something yourself using euler angles, you should keep the “real” value yourself. Otherwise rotate via quaternions. (Mathf.MoveTowardsAngle has a bad example.)

Secondly, Transform.RotateAround()'s third parameter is the amount to rotate. It is not the angle that it should arrive at. Using currentYAngle (or transform.eulerAngles.y) won’t provide any sane results, because that is the object’s own rotation, and has no connection to this new Vector3(0f,6.73f,30f) point.

Here is a solution with some trigonometry:

using UnityEngine;
using System.Collections;

public class rotatearoundme : MonoBehaviour
{
    public float rotationSpeed = 360f;

    public float distance = 5f;

    public Vector3 pivot = new Vector3(0f, 6.73f, 30f);

    private float currentYAngle = 0f;

    private float targetYAngle = 0f;

    // Use this for initialization
    void Start()
    {
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.Alpha1))
            targetYAngle = -90;
        else if (Input.GetKey(KeyCode.Alpha2))
            targetYAngle = 0;
        else if (Input.GetKey(KeyCode.Alpha3))
            targetYAngle = 90;
        else if (Input.GetKey(KeyCode.Alpha4))
            targetYAngle = 180;

        currentYAngle = Mathf.MoveTowardsAngle(currentYAngle, targetYAngle, rotationSpeed * Time.deltaTime);

        transform.position = new Vector3(
            pivot.x + Mathf.Sin(currentYAngle * Mathf.Deg2Rad) * distance,
            transform.position.z,
            pivot.z + Mathf.Cos(currentYAngle * Mathf.Deg2Rad) * distance
        );
    }
}

Here you would treat currentYAngle/targetYAngle as the angle towards the pivot (and not the object’s rotation, although it would be very similar if it had to face the pivot).

Mathf.Sin(currentYAngle * Mathf.Deg2Rad) * distance, is

37914-sin_cos_01.gif

where R is distance and theta is currentYAngle.