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?
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.
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);
}
}
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?