Make object rotate around target with increasing size

Hey, sorry for my english.
My script should be simple, but there’s a problem i can’t manage to solve.
I want a object to rotate around a target when it detects it.
The problem is,this target increases with size, and when it does, the object rotating around it is not increasing it’s rotation radius.
I don’t know how to solve it.

My script ( i just call this on update):

void checkCol(){
         //Instancia o RayCast
        RaycastHit hit;
         Debug.DrawRay(transform.position, transform.forward * rayDistance, Color.red);

        if(Physics.Raycast(transform.position,transform.forward,out hit,rayDistance,Layers)){
            Debug.Log(hit.collider.name);
            if(hit.collider.name == "lightShield"){
                moveSpeed = 0f;
                transform.RotateAround(hit.collider.transform.position, randomRotationDirection, rotateSpeed * Time.deltaTime);

            }

        }
    }

You need to push your object away from the growing object at the same rate as it grows while it grows. This is getting the direction between your object and the object it’s rotating around, and repositioning in the opposite of that direction.

1 Like

I never head of that. I’ll try to find a gud on the web (but i’m not having much success)
Thanks for the answer.

RotateAround on it’s own won’t cut it here.

You’ll need to have a directional vector, rotate that vector and add that direction to your position.

Then as your object grows you can add it’s radius to your directional vector to slowly increase the distance.

I’ll show you how to do the rotating part to illustrate the concept:

public class OrbitBehaviour : Monobehaviour
{
    public float rotationSpeed = 5f;

    public Transform rotateObject; //assign the orbiting object here

    private Vector3 direction = Vector3.forward;

    private void Update()
    {
        float delta = rotationSpeed * Time.deltaTime;
        Quaternion orbitRotation = Quaternion.AngleAxis(delta, transform.up); //make a rotation around an axis
      
        direction = orbitRotation * direction; //rotate your direction
      
        Vector3 orbitPosition = transform.position + direction; //get your position around this game object
      
        rotateObject.transform.position = orbitPosition;
    }
}

Ergo we start with a direction and rotate it each frame.

To do the ‘growing’ part, I would make a component for your satellite object and use that in place of public Transform rotateObject;. Have it update itself each update its size each frame, with some public value to indicate it’s size.

Then you just mutate the direction vector (such as just multiplying simply) before you make orbitPosition.