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:
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;
}
}