- Home /
Rotate 2D Sprite Towards Moving Direction
Hi there! This question has been answered, the solution is to use parent GameObject, but in my case, I can't do it, as I'm using specific GUI sprite.
I have this code to move it along the path and rotate:
iTween.MoveTo (this.gameObject,iTween.Hash("oncomplete", "SeekNewPlace", "path",iTweenPath.GetPath("ButterflyPath"), "time", 10f, "easetype", iTween.EaseType.linear));
void Update()
{
Vector3 moveDirection = this.gameObject.transform.position - _origPos;
if (moveDirection != Vector3.zero)
{
this.gameObject.transform.rotation = Quaternion.LookRotation(moveDirection);
// and afterward, if you want to constrain the rotation to a particular axis- in this case Y:
this.gameObject.transform.eulerAngles = new Vector3(0f, 0f, transform.eulerAngles.x -90f);
}
}
But no matte what, sprite is not facing correct direction for me. Maybe there is something else I could use instead of Quaternion for this 2D sprite case?
Answer by robertbu · Feb 02, 2014 at 08:12 PM
Assuming you are using a sprite viewed from a camera with rotation (0,0,0) and the sprite's right side is considered the 'forward' of the sprite, you can do it this way:
void Update()
{
Vector3 moveDirection = gameObject.transform.position - _origPos;
if (moveDirection != Vector3.zero)
{
float angle = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
Note '_origPos' is never being updated in this code. Not sure if this is by design or an oversight.
Note that this works until a parent object's scale is flipped. Using transform.localRotation seems to have no effect :(
Very nice! I was trying to create the Quaternions directly, but had no idea how to do left and right. But I got up and down!!
// up this.transform.rotation = new Quaternion(0,0,0,0); // down this.transform.rotation = new Quaternion(0,0,180,0);
Answer by Jokeman258 · Mar 01, 2015 at 01:21 AM
Use this instead. It's a simpler version specifically for 2D use so you don't have to calculate the _origPos
void Update () {
Vector2 moveDirection = gameObject.rigidbody2D.velocity;
if (moveDirection != Vector2.zero) {
float angle = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
This helped me out a bit on my project, So thank you sir!
same, I keep getting a conflict with the float-fader and Quaternion.identity.
Answer by kdawgfsho04 · Nov 27, 2020 at 08:43 PM
how do i get it so my sprite doesnt lay sideways when i do this i want my sprite to stay upright when i try to lock in rigid body i get no luck when it changes position it lays sideways i want to keep him straight up anyway to do thiis?
If you have a sprite that is looking up instead of right, use robertbu's answer and simply subtract 90 from Mathf.Rad2Deg, e.g:
float angle = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg - 90;
Your answer
Follow this Question
Related Questions
2D sprite rotation with velocity and interpolation 1 Answer
Change Sprite Origin in 2D 1 Answer
Sprite is a little of when rotating 1 Answer