Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rotating a 2D object

Discussion in '2D' started by Umbra_1, Jul 17, 2017.

Thread Status:
Not open for further replies.
  1. Umbra_1

    Umbra_1

    Joined:
    Jul 17, 2017
    Posts:
    2
    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.
     
    CadetChurro likes this.
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    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:
    Code (CSharp):
    1. transform.eulerAngles = Vector3.forward * degrees;
    2. // or
    3. transform.rotation = Quaternion.Euler(Vector3.forward * degrees);
    4. // or
    5. transform.rotation = Quaternion.LookRotation(Vector3.forward, yAxisDirection);
    6. // or
    7. transform.LookAt(Vector3.forward, yAxisDirection);
    8. // or
    9. transform.right = xAxisDirection;
    10. // or
    11. 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)"
     
  3. Umbra_1

    Umbra_1

    Joined:
    Jul 17, 2017
    Posts:
    2
    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 :

    Code (CSharp):
    1. transform.Rotate(Vector3(0, 0, 50));
    2. //instead of :
    3. transform.eulerAngles = new Vector3 (0, 0, 50);
    4. //or like you said
    5. transform.eulerAngles = Vector3.forward * 50;

    Thanks again for the quick answer!
     
  4. dreek16designer

    dreek16designer

    Joined:
    Mar 21, 2020
    Posts:
    8
    isn't it supposed to be Vector2?
     
  5. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    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.
     
    damobe likes this.
  6. dreek16designer

    dreek16designer

    Joined:
    Mar 21, 2020
    Posts:
    8
  7. shadyj

    shadyj

    Joined:
    May 21, 2020
    Posts:
    6
    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
     
  8. shadyj

    shadyj

    Joined:
    May 21, 2020
    Posts:
    6
    its 2d and I want to make a counting system for the score and also how many balls you have goon on
     
  9. shadyj

    shadyj

    Joined:
    May 21, 2020
    Posts:
    6
    but the rotation is not working ad I cant make the ball can't orbit and the enemy cant attach to the big ball
     
  10. shadyj

    shadyj

    Joined:
    May 21, 2020
    Posts:
    6
    so can any of you help me
     
  11. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    @shadyj Please create your own thread instead of hijacking this one.
    Also, condense everything into one post please - this is a forum, not a chatroom.
     
  12. shadyj

    shadyj

    Joined:
    May 21, 2020
    Posts:
    6
    sorry
     
  13. TheBosoYolo

    TheBosoYolo

    Joined:
    Mar 9, 2021
    Posts:
    1
    Hi, When my game is 2d I'll allways keep Z at zero? Thanks for responding
     
  14. xqtr123

    xqtr123

    Joined:
    Jun 7, 2014
    Posts:
    20
    This code worked for me:



    Code (CSharp):
    1.  
    2.     private void Awake()
    3.     {
    4.         StartCoroutine(RotateMe(Vector3.back * 90, 0.8f));
    5.     }
    6.  
    7.     IEnumerator RotateMe(Vector3 byAngles, float inTime)
    8.     {
    9.         var fromAngle = transform.rotation;
    10.         var toAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
    11.         for (var t = 0f; t < 1; t += Time.deltaTime / inTime)
    12.         {
    13.             transform.rotation = Quaternion.Slerp(fromAngle, toAngle, t);
    14.             yield return null;
    15.         }
    16.     }
    17.  
     
  15. Kokomom

    Kokomom

    Joined:
    Mar 23, 2021
    Posts:
    1
    hi,
    I can't figure out how to rotate a 2d object can someone please tell me
     
  16. adrianfrancisco

    adrianfrancisco

    Joined:
    Jul 29, 2021
    Posts:
    14

    Thank you so much!!!!!!!!!!!!!
     
Thread Status:
Not open for further replies.