- Home /
Sprites rotating around Z axis when they shouldnt be.
Hi,
I have a script where a sprite will follow the player. It works, other than the sprite rotating around the Z axis.
Can someone help me with trying to make it 2D and not spin around the Z axis please.
This is the code that is controlling the movement:
Thanks
void Start()
{
myTransform = transform;
player = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update ()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
follow=true;
if(follow && player != null)
{
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(player.position - myTransform.position), rotationSpeed * Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
Read carefully about what LookRotation does
Usually, and I think it applies here, to eli$$anonymous$$ate an unwanted axis of orientation, eli$$anonymous$$ate the "height" component of the argument vector. You may also supply a "normal" to this method, which should often be the normal of the plane on which these two objects lie (or Vector3.forward in a 2D game following Unity's z-is-depth pillar).
Your answer
![](https://koobas.hobune.stream/wayback/20220613200402im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Edit tilemap cillider edit z hight 0 Answers
How to give a direction to a hook? 1 Answer
2D going through walls bug 1 Answer
RigidbobyConstraints2D is not working in Unity 5.0.2f1 Personal Edition!! 0 Answers
Z order for GameObject 0 Answers