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

2d game flip game object on x axis to face the other way based on mouse position

Discussion in 'Scripting' started by uistudios, Nov 18, 2012.

  1. uistudios

    uistudios

    Joined:
    Apr 6, 2009
    Posts:
    46
    Hi Guys, How do I go about flipping a character on the X axis in a 2D game?
    I have a shooter named "Player" that follows the mouse and i need this guy to do something like

    if mouse X position is less then Player's X position flip the Player Game Object to face left and VS
    so another words as the player looks and aims at the mouse he also looks good when the mouse goes behind the player ;)
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    Object.transform.localScale.x = -1;
     
  3. uistudios

    uistudios

    Joined:
    Apr 6, 2009
    Posts:
    46
    I can't get this to work... is there some sort of game example or tutorial that shows how to flip an object based on mouse position? I am sure this is pretty simple but i can't figure it out :(
     
  4. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    You can't directly change the x value of localscale. You need to create a temporary vector3, assign the localscale, modify it and reassign the updated vector3 back to the localScale.
    Code (csharp):
    1.  
    2. Vector3 newScale = obj.transform.localScale;
    3. newScale.x *= -1;
    4. obj.transform.localScale = newScale;
    5.  
    *EDIT*
    To check the location of the mouse in world space, use ScreenToWorldPoint to convert the mouse position and compare the x of both the returned value of the mouse position and your object's position.
     
  5. uistudios

    uistudios

    Joined:
    Apr 6, 2009
    Posts:
    46
    thank you for your help guys!
    this seems to work for me

    transform.Rotate(new Vector3(0,180,0));
     
  6. Like-A-Sir

    Like-A-Sir

    Joined:
    Sep 14, 2015
    Posts:
    33
    Thank you!

    Your little line of code helped me now in 2016 :d !
     
  7. brian-nielsen

    brian-nielsen

    Joined:
    Apr 18, 2018
    Posts:
    15
    Hi All,

    First time posting. The above helped me but I noticed that if I had a more complex object with children it helped me to use

    Code (CSharp):
    1. transform.rotation = Quaternion.Euler(0, 180f, 0);
    to face right and when I wanted to face left again:

    Code (CSharp):
    1. transform.rotation = Quaternion.Euler(0,0,0);
    I used this Unity Manual page https://docs.unity3d.com/Manual/QuaternionAndEulerRotationsInUnity.html

    Hope this helps someone else in the future.
     
    Tamze, Thatila, FreeDis and 3 others like this.
  8. JPaz0223

    JPaz0223

    Joined:
    Mar 31, 2018
    Posts:
    1

    It just did
     
  9. Kaboka22

    Kaboka22

    Joined:
    Oct 1, 2019
    Posts:
    1
    Sometimes you don`t want to rotate it because this happens: Untitled3.png

    The way to make this work is just simply scaling it in it`s X axis like this:
    Code (CSharp):
    1. transform.localScale = new Vector3(-1f, 1f, 1f);
    Hope this helps a bit!
     
  10. RevengerWizard

    RevengerWizard

    Joined:
    Feb 15, 2020
    Posts:
    15

    The problem with this is that the childs attached to the player will be scaled, like the UI, weapons, ecc
     
  11. tonycrisiony

    tonycrisiony

    Joined:
    Mar 24, 2020
    Posts:
    1
    So what would be the solution in this case?