Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Lookat in 2D

Discussion in 'Scripting' started by MG, Aug 23, 2014.

  1. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    Hello.

    I got an Object which I want to be scripted so the Object is Looking at the Mouse.

    I have tried alot of script, but non of them seems to be working with me
     
  2. Dave-xP

    Dave-xP

    Joined:
    Nov 8, 2013
    Posts:
    106
    It's just some math...
    Code (CSharp):
    1. transform.eulerAngles = new Vector3(0,0,Mathf.Atan2(Input.mousePosition.x - Screen.width/2 - transform.position.x, Input.mousePosition.y - Screen.height/2 - transform.position.y)*Mathf.Rad2Deg);
    This will let your transform "look at" the mouse cursor.