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 rotation to face player at mouse

Discussion in 'Scripting' started by tookydo, Aug 1, 2014.

  1. tookydo

    tookydo

    Joined:
    Jul 15, 2014
    Posts:
    75
    I am trying to make a 2d top down shooter and i want the controls to be where you can just move forward and backward and then you can move the mouse to rotate the player to face the mouse. I have an empty game object that move where the mouse is. This is what my code looks like:

    public var target : Transform;

    function Update()
    {
    transform.LookAt(target);
    }

    I thought this would work. Everything that I tried was buggy. This will move the camera back and forth between the front of the player and behind. It also doesn't rotate enough to be pointing at the mouse. Please help!
     
  2. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    I believe it's because LookAt rotates an object on the z-axis by default. You need to specify a different axis since you're using 2D.
     
  3. tookydo

    tookydo

    Joined:
    Jul 15, 2014
    Posts:
    75
  4. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
  5. lrlelaldl

    lrlelaldl

    Joined:
    Jul 27, 2014
    Posts:
    75
    You also do the math yourself and set the rotation that way?