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

How do i flip FromToRotation on a certain axis?

Discussion in 'Scripting' started by medofox321, Jun 17, 2022.

  1. medofox321

    medofox321

    Joined:
    Nov 6, 2020
    Posts:
    3
    I am using FromToRotation to rotate towards an object in a 2D game, however, my Yaxis is facing downwards and i don't know how to flip it without messing the rotation up.
    Code(update method):

    Code (CSharp):
    1. Vector2 direction = Player.position - new Vector3(Shoulder.transform.position.x, Shoulder.transform.position.y, Shoulder.transform.position.z);
    2.  
    3. Shoulder.transform.rotation = Quaternion.FromToRotation(Vector3.right, direction);
    Note: the black capsule is not the player.

    example.png
     
  2. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    Use Vector3.left instead of Vector3.right (or -Vector3.right)
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    I'll move your post to the scripting forum, it does not relate to either 2D or 3D physics.
     
    r31o likes this.
  4. medofox321

    medofox321

    Joined:
    Nov 6, 2020
    Posts:
    3
    It changes the direction am rotating to, not the rotation of the x axis, instead of pointing at the player, it points exactly opposite from the player...
     
  5. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    Try with this (put it AFTER the rotation code)
    Code (CSharp):
    1. transform.up = -transform.up
    This should flip it
     
  6. medofox321

    medofox321

    Joined:
    Nov 6, 2020
    Posts:
    3
    It flipped the entire thing, using transform.right = -transform.right also did the same


    Edit: Code:


    Code (CSharp):
    1.             Vector2 direction = new Vector3(Player.position.x,Player.position.y, 0) - new Vector3(Shoulder.transform.position.x, 0, Shoulder.transform.position.z);
    2.  
    3.             Shoulder.transform.rotation = Quaternion.FromToRotation(Vector3.right, direction);
    4.             Shoulder.transform.up = -Shoulder.transform.up;
     

    Attached Files:

  7. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    I guess that the pivot of the square is not in the center.
    Scale it by -1 in local Y axis