Set Velocity of Object According to Rotation

I am writing a program such that there is a rotating object and the player can attach to that rotating object and also rotate accordingly. After the click of a button the player is supposed to move in the direction where his head is. I’ve looked online and found some answers but none worked for me. Here are some example pictures:
165953-screenshot-6.png
165952-screenshot-4.png

Below is the code for my rotating platform:

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

Below is the code for my player to attach and release to the platform:
to attach:

  void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject.name == "head"&&canCollide)
        {
            canMove = false;
            rb.velocity = new Vector2(0, 0);
            transform.rotation = col.gameObject.transform.parent.rotation;
            transform.parent = col.gameObject.transform.parent;
            float difY = transform.position.y - transform.parent.position.y;
            float difX = transform.position.x - transform.parent.position.x;
            Vector3 temp = new Vector3(difX, difY,0);
            transform.position = new Vector3(transform.position.x,transform.position.y,transform.position.z)+temp.normalized;

            rb.gravityScale = 0;
            canCollide = false;
        }
    }

to release(this is the part where I need to set my velocity according to my rotation I’ve got this code from another post but didn’t work in my case):

 void blastFromSkyHook()
    {
        if (transform.parent != null && Input.GetKeyDown("m"))
        {

            transform.rotation = transform.parent.rotation;
            transform.parent = null;
            float theta = transform.localEulerAngles.z +90;
            rb.gravityScale = 0.3f;
            rb.AddForce(new Vector2(1,1) * 500);
            
            float newDirX = Mathf.Cos(theta * Mathf.Deg2Rad);

            float newDirY = Mathf.Sin(theta * Mathf.Deg2Rad);

            rb.AddRelativeForce(new Vector2(newDirX, newDirY)); 
            Debug.Log(rb.velocity);
            canMove = true; 
        }
    }

It looks like you’re trying to overengineer something that can be handled in much simpler(-looking) terms.

For now, let’s take a look at your blastFromSkyHook() function.

To start, this line doesn’t really make much sense and is probably the source of the erroneous results you’re seeing:

rb.AddForce(new Vector2(1,1) * 500);

That will launch your character at (default) ~14.14 units/second (sqrt(2) * 500 / 50). I say “default” because you’re applying a per-frame force as an immediate one; I’ll go into detail on that later.

Then, you apply a directional force extremely weakly after that (it will have almost no visible effect on the trajectory you had just set prior).

rb.AddRelativeForce(new Vector2(newDirX, newDirY));

You can easily approach this from two different angles (at least). You can either:

A) use AddForce(global direction)

or
B) use AddRelativeForce(local direction) for the same effect

But before that, let’s go back to what I mentioned before. You’re applying a per-frame force right now. That’s because AddForce() (and similar) use the ForceMode2D of Force by default.

rb.AddForce(Vector2.one);
// is the same as...
rb.AddForce(Vector2.one, ForceMode2D.Force);

However, the way you’re trying to use it indicates you’re applying the intended force a single time. Well, the default physics rate is 50 times per second. If you change the physics update rate (from 0.2), then you’ll suddenly apply more or less force to your character! Clearly, that isn’t what you had in mind, so let’s look at the alternative: ForceMode2D.Impulse, which applies the force immediately instead:

// This assumes the intended "forward" direction is "up"
// based on 90-degree offset
rb.AddForce(transform.up * launchSpeed * rb.mass, ForceMode2D.Impulse);
// or...
rb.AddRelativeForce(Vector3.up * launchSpeed * rb.mass, ForceMode2D.Impulse);

With either of those, you can also greatly simplify the function:

void BlastFromSkyHook()
{
	transform.rotation = transform.parent.rotation;
	transform.parent = null;
	rb.AddRelativeForce(Vector3.up * launchSpeed * rb.mass, ForceMode2D.Impulse);
	rb.gravityScale = 0.3f;
	canMove = true;
}

You might notice that I’m also factoring the Rigidbody’s mass into the calculations. This ensures that changing the character’s mass will have no bearing on these launches.

Here is my run method @Eno-khan :
//terminalVelocity is how fast player can go by user input

void run()
{
    float direction = Input.GetAxisRaw("Horizontal");
    if ((rb.velocity.x > terminalVelocity && direction > 0) || (rb.velocity.x < -terminalVelocity && direction < 0))
        return;
    rb.AddForce(new Vector2(direction * xSpeed, 0));
}