How to get the angle a 2D character is facing?
Hello, I am making a game where the main character can transfer to flying mode when jumping at a high speed. But I have tried so much to get this flight working.
I want him to slow down when going towards the sky and speed up when going towards the ground.
this isn't working, but here is my code
if (flyingRight)
{
if (isPitching)
{
rb.AddTorque(flightInput * 0.5f);
}
Debug.Log(transform.rotation.z);
//
//if (transform.rotation.eulerAngles.z > 0)
if (gameObject.transform.localEulerAngles.z > 0 && gameObject.transform.localEulerAngles.z < 180)
{
flightSpeed -= flightGravity * Time.deltaTime;
//flightSpeed = 0;
// Debug.Log(flightSpeed + "test");
} else if (gameObject.transform.localEulerAngles.z < 360 && transform.rotation.z > 180)
{
flightSpeed += flightGravity * Time.deltaTime;
//flightSpeed = 0;
//Debug.Log(flightSpeed + "test");
}
GetComponent<Rigidbody2D>().velocity = transform.right * flightSpeed;
Comment
Best Answer
Answer by Amras789 · Apr 18, 2020 at 03:12 AM
I ended up getting it to work by making two empty game objects, one that stays to the relative right and another that always stays above the player. I took the angle between the two and it works perfectly
direction = upDirection.position - transform.position; rightDirection = facingDirection.position - transform.position; angle = Vector2.Angle(rightDirection, direction);