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

How to make a 2D gameobject face another?

Discussion in 'Scripting' started by HaydarLion, Aug 18, 2016.

  1. HaydarLion

    HaydarLion

    Joined:
    Sep 17, 2014
    Posts:
    62
    Hey all,

    I am having trouble finding a way to write some code (c#) that would force one 2D gameobject to face another. I am using transform.localscale.x as my direction that it is facing (with 1 as facing right, and -1 as facing left). Can anyone provide a solution to this problem?

    Thanks.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. HaydarLion

    HaydarLion

    Joined:
    Sep 17, 2014
    Posts:
    62
    The thing about transform.lookat is that it rotates the sprite. In this case, I don't want the position or rotation to be effected but rather just the localscale.x so that the sprite is flipped based on which direction it is facing. This method has proven itself to work well, as it also causes the children objects to be flipped. So I need a way to change the scale based whether the object its trying to face is on the left or right of it.