Search Unity

how can "lookat" a sprite?

Discussion in '2D' started by moscoquera, Nov 16, 2013.

  1. moscoquera

    moscoquera

    Joined:
    Nov 16, 2013
    Posts:
    4
    hi.

    i'm using two GameObjects with SpriteRenderer, and i need that the first GameObject Look at the second GameObject. i tried using "TransForm.Lookat" but it only works in 3D mode. :(
     
    Last edited: Nov 16, 2013
  2. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    What does LookAt mean in context to your project? Are you making a top-down or platform style? What is the "desired" effect you wish to accomplish?
     
  3. moscoquera

    moscoquera

    Joined:
    Nov 16, 2013
    Posts:
    4
    i'm doing a platform game. I have rocket and a target (both are GameObjects), and i need that the rocket sight the target
     
  4. moscoquera

    moscoquera

    Joined:
    Nov 16, 2013
    Posts:
    4
    i finally solved it:

    Code (csharp):
    1. public static Quaternion lookat(Vector3 from, Vector3 target, Vector3 forwardDirection){
    2.         return Quaternion.FromToRotation(forwardDirection, target - from);
    3.     }
    from and target are position points, and forwardDirection is the forward vector relative to the "from" point.