How to make a game object move to the opposite direction of a constantly moving object?

I have a game object that constantly rotates around another one:
198064-ezgifcom-gif-maker.gif

What I am trying to achieve is to have the circle move in the opposite direction of the square when I press the space key, the square will simulate a propeller, for example:

198065-markup-2022-07-26-at-23422-pm.png

I have tried the following code and it kind of works:

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

public class PlayerController : MonoBehaviour
{

    [SerializeField] GameObject rotatingPropellant;
    [SerializeField] float rotationSpeed;
    [SerializeField] float impulseForce;


    Rigidbody2D playerRb;

    void Start()
    {
        playerRb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        RotatePropellant();
        Move();
    }

    void RotatePropellant()
    {
        rotatingPropellant.transform.RotateAround(transform.position, new Vector3(0f, 0f, 1f), rotationSpeed * Time.deltaTime);
    }

    void Move()
    {
        if (Input.GetButtonDown("Jump"))
        {
            playerRb.velocity = -rotatingPropellant.transform.position * impulseForce * Time.deltaTime;
        }

    }
}

But sometimes the circle moves up even if the square is up (meaning that the circle should be pushed down), not sure if maybe the signs are not correct or if I should be approaching this with a different method

Hi!

Your code was taking the absolute position of the propeller and boosting the player in that direction, the result is that your player always get pushed towards the world origin. If you add some still reference object there, you will see that that’s the case.

The fix is quite simple, you just need to take into account the players position in relation to the propeller:

Vector2 receedingVector = playerRb.position - (Vector2)rotatingPropellant.transform.position;
playerRb.velocity = receedingVector.normalized * impulseForce * Time.deltaTime;

You can also normalize that vector if your goal is to have only the impulseForce variable influence the speed and not the orbiting speed of the propeller.

Hope this helps :smiley: