How can I rotate one object around another object, but freeze rotation?

I’m trying to rotate an enemy around the player object, but when I do this the enemy sprite itself rotates around.

I want to freeze its rotation, or perhaps rotate itself backwards to compensate? What’s the solution here?

Here’s what I have so far.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemySeagull : MonoBehaviour
{
    public float moveSpeed;
    public float orbitDistance;
    public float rangeToAnnoyPlayer;
    private Transform target;
    private bool isInRange;
    private Vector3 zAxis = new Vector3(0,0,1);

    // Start is called before the first frame update
    void Start()
    {
        isInRange = false;
        target = PlayerController.instance.transform;
    }

    // Update is called once per frame
    void Update()
    {
        if (!isInRange)
        {
            if (Vector2.Distance(transform.position, PlayerController.instance.transform.position) < rangeToAnnoyPlayer)
            {
                isInRange = true;
            }
        }


        if (isInRange)
        {
            transform.position = target.position + (transform.position - target.position).normalized * orbitDistance;

            transform.RotateAround(PlayerController.instance.transform.position, zAxis, 120 * Time.deltaTime);

        }
        else
        {

            transform.position = Vector3.MoveTowards(transform.position, target.position, 3 * Time.deltaTime);
        }



        Debug.Log("isInRange = " + isInRange);

    }


}

Hey,

So one way you could do this is:

  1. Put the enemy sprite in an empty object and move the enemy sprite away from its parent.
  2. Have the empty object follow the player.
  3. Rotate the empty object (this will cause the enemy sprite to rotate around the player).
  4. Reset the world rotation of the enemy sprite every frame.

I tested this quickly and it does work. Here’s the enemy rotation script I used:

public class EnemyRotation : MonoBehaviour
{
    [SerializeField] float rotSpeed = 10f;

    void Update()
    {
        transform.Rotate(0f, 0f, rotSpeed * Time.deltaTime);
    }
}

Put this on the empty object that has the enemy sprite as a child.

Here is the code to reset the world rotation of the enemy sprite:

public class ResetWorldRotation : MonoBehaviour
{
    void Update()
    {
        transform.rotation = Quaternion.Euler(0f, 0f, 0f);
    }
}

transform.localRotation will set the rotation relative to any parent objects, but transform.rotation will always use the actual world’s rotation.

This is just one way of doing it I thought up quickly, hopefully it’s of some help! Good luck!