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. Dismiss Notice

transform.Rotate not working as it should

Discussion in '2D' started by Alexiiinka, Jul 7, 2022.

  1. Alexiiinka

    Alexiiinka

    Joined:
    Jul 27, 2021
    Posts:
    6
    Hello, so I have this problem - I want my "fish" to move right/left and up/down. It should change direction when its position.x is more/less than... so it doesnt "jump off" aquarium. The movement right/left was all okey, but when I implemented up and down rotation (in my case z axis) it sometimes wont work as it should. I really dont know why, sometimes the fish rotates perfectly and another time it rotates weird. I also saw that the fish rotation on x axis changed from 0 to 180 but in my code I dont rotate x...
    My code:
    Code (CSharp):
    1. //to know if we go right (1,1) or left (-1,1),     up (1,1) or down (1,-1)
    2.     private Vector2 rightMove = new Vector2(1,1), upMove = new Vector2(1,1);
    3.     private Vector2 diagonalVector = new Vector2(0.5f, 0.5f); //diagonal move
    4.     private bool leftMoving = false, upMoving = true;
    5.  
    6.     protected override void Move()
    7.     {
    8.          if (transform.position.y < -4 && !upMoving) // goes down, we want up
    9.         {
    10.             transform.Rotate(0, 0, 90);
    11.             upMoving = true;
    12.             upMove = new Vector2(1, 1);
    13.         }
    14.         else if (transform.position.y > -1 && upMoving) // goes up, we want down
    15.         {
    16.             transform.Rotate(0, 0, -90);
    17.             upMoving = false;
    18.             upMove = new Vector2(1, -1);
    19.         }
    20.         if (transform.position.x < -8 && leftMoving) //goes left, we want right
    21.         {
    22.             rightMove = new Vector2(1,1);
    23.             leftMoving = false;
    24.             transform.Rotate(0,180,0);
    25.         }
    26.         else if (transform.position.x > 8 && !leftMoving) // goes right, we want left
    27.         {
    28.             rightMove = new Vector2(-1,1);
    29.             leftMoving = true;
    30.             transform.Rotate(0,180,0);
    31.         }
    32.  
    33.      
    34.         // movement
    35.         rb.velocity = diagonalVector * speed * upMove * rightMove;
    upload_2022-7-7_17-13-40.png

    Thanks for help
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    There is a maths reason that your X rotation is changing as a result of the Rotate operation since you're altering both Z and Y rotations.

    There are a ton of ways to do this kind of motion & rotation, but I'll give you some simple things to try based on how you're currently doing it.
    1. Only rotate on Z, never do Y rotation. Instead, invert your Y scale to flip the sprite upside down. (or experiment with SpriteRenderer.flipY). That should make sure your X and Y rotations never change.
    2. Instead of doing relative rotations, set the rotation directly like this:
      transform.rotation = Quaternion.Euler(x,y,z);
      or
      transform.eulerAngles = new Vector3(x,y,z);

    I would suggest you make sure your fish faces along the X axis by default (when no rotation is applied). Then you don't have to account for the default diagonal offset.

    Here's an example for your consideration:
    Code (CSharp):
    1. private bool leftMoving = false, upMoving = true;
    2. private Vector2 movementDirection = Vector2.one;
    3. private SpriteRenderer fishSpriteRenderer;
    4.  
    5. protected override void Move()
    6. {
    7.     if (transform.position.y < -4 && !upMoving) // goes down, we want up
    8.     {
    9.         movementDirection.y = 1;
    10.         upMoving = true;
    11.     }
    12.     else if (transform.position.y > -1 && upMoving) // goes up, we want down
    13.     {
    14.         movementDirection.y = -1;
    15.         upMoving = false;
    16.     }
    17.     if (transform.position.x < -8 && leftMoving) //goes left, we want right
    18.     {
    19.         movementDirection.x = 1;
    20.         leftMoving = false;
    21.     }
    22.     else if (transform.position.x > 8 && !leftMoving) // goes right, we want left
    23.     {
    24.         movementDirection.x = -1;
    25.         leftMoving = true;
    26.     }
    27.  
    28.     // flip the sprite when moving left
    29.     fishSpriteRenderer.flipY = leftMoving;
    30.     // movement
    31.     rb.velocity = movementDirection.normalized * speed;
    32.     // point the X axis in the velocity direction
    33.     transform.right = rb.velocity.normalized;
    34. }
    Hope that helps give you some ideas for how to fix your issues.
     
    Alexiiinka likes this.
  3. Alexiiinka

    Alexiiinka

    Joined:
    Jul 27, 2021
    Posts:
    6
    Thanks very much for detailed reply. I've learned much from this :) It worked.
     
    LiterallyJeff likes this.