# Rotating a 2D object

Hi, I’d like to rotate a 2D sprite when a key is pressed ( a ship loooking left when Left Arrow is pressed, up when Up Arrow is pressed etc ) but I don’t get how to SET a rotation when the key is pressed, every things i tried made the ship rotating 90° to a direction and he kept rotating while i was pressing the key, while i just want it to rotate to a certain value and then stop.
I tried transform.Rotate ; transform.rotateAround ; euler ; quaternions and some others.
So do someone have a solution for this ?
Sorry if there’s mistakes, I’m not English.

1 Like

Your issue is that Rotate functions will move from the current rotation by value you give it. They will always offset the rotation when called.

Setting a rotation in 2D comes in many forms:

``````transform.eulerAngles = Vector3.forward * degrees;
// or
transform.rotation = Quaternion.Euler(Vector3.forward * degrees);
// or
transform.rotation = Quaternion.LookRotation(Vector3.forward, yAxisDirection);
// or
transform.LookAt(Vector3.forward, yAxisDirection);
// or
transform.right = xAxisDirection;
// or
transform.up = yAxisDirection;
``````

Just FYI, “Vector3.forward” is virtually the same as “new Vector3(0, 0, 1)”. I multiply by this because I want to end up with a Vector that only has a Z value (because in 2D you rotate on the Z axis).

So “Vector3.forward * 50” will equal “Vector3(0, 0, 50)”

14 Likes

Thanks alot ! My problem was that i didn’t know that i had to create a new Vector3 ( well i didn’t even know that it was a variable when I posted this ), I tried for example :

``````transform.Rotate(Vector3(0, 0, 50));
transform.eulerAngles = new Vector3 (0, 0, 50);
//or like you said
transform.eulerAngles = Vector3.forward * 50;
``````

Thanks again for the quick answer!

5 Likes

isn’t it supposed to be Vector2?

In Unity, there’s functionally no difference between a 2D game and a 3D game. It’s just the difference in camera perspective and the use of 2D components instead of 3D components.

Because the z-axis is the one that is facing towards a 2D camera perspective, that’s the axis you use to make an object rotate in how you would typically expect in a 2D game.

You can still use the x and y axes to make a 2D object rotate in 3D space.

1 Like

Ok

oh I am trying to make a video game and I want to make it infinite its a game where your a ball on a ball ovoiding squares and getting to the next ball. I need help making it loop and making It infinite and I use visual studio and its not working

its 2d and I want to make a counting system for the score and also how many balls you have goon on

but the rotation is not working ad I cant make the ball can’t orbit and the enemy cant attach to the big ball

so can any of you help me

Also, condense everything into one post please - this is a forum, not a chatroom.

4 Likes

sorry

Hi, When my game is 2d I’ll allways keep Z at zero? Thanks for responding

This code worked for me:

``````    private void Awake()
{
StartCoroutine(RotateMe(Vector3.back * 90, 0.8f));
}

IEnumerator RotateMe(Vector3 byAngles, float inTime)
{
var fromAngle = transform.rotation;
var toAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
for (var t = 0f; t < 1; t += Time.deltaTime / inTime)
{
transform.rotation = Quaternion.Slerp(fromAngle, toAngle, t);
yield return null;
}
}
``````

hi,
I can’t figure out how to rotate a 2d object can someone please tell me

Your issue is that Rotate functions will move from the current rotation by value you give it. They will always offset the rotation when called.

Setting a rotation in 2D comes in many forms:

``````transform.eulerAngles = Vector3.forward * degrees;
// or
transform.rotation = Quaternion.Euler(Vector3.forward * degrees);
// or
transform.rotation = Quaternion.LookRotation(Vector3.forward, yAxisDirection);
// or
transform.LookAt(Vector3.forward, yAxisDirection);
// or
transform.right = xAxisDirection;
// or
transform.up = yAxisDirection;
``````

Just FYI, “Vector3.forward” is virtually the same as “new Vector3(0, 0, 1)”. I multiply by this because I want to end up with a Vector that only has a Z value (because in 2D you rotate on the Z axis).

So “Vector3.forward * 50” will equal “Vector3(0, 0, 50)”

[/quote]

Thank you so much!!!

1 Like