Search Unity

How to make a spotlight look at the mouse

Discussion in 'Scripting' started by SHIMMY, Jun 2, 2014.

  1. SHIMMY

    SHIMMY

    Joined:
    Dec 28, 2012
    Posts:
    107
    So I have a spotlight but I need it to stay in one place - on the ground.Whilst the beam (the end) needs to follow and point at the mouse. I have absolutely no idea how to do this. So the beam kind of needs to rotate around an anchor at it's source.

    All I can find is how to make the light follow the mouse at its source. Rather than just the beam facing the mouse.

    Any help is greatly appreciated.
     
    Last edited: Jun 2, 2014
  2. hexagonius

    hexagonius

    Joined:
    Mar 26, 2013
    Posts:
    98
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    I would use ScreenPointToRay, not ViewportToWorldPoint. then simply get the point out that you need.

    Code (csharp):
    1.  
    2. point = Camera.main.ScreenPointToRay(Input.mousePosition).GetPoint(10);
    3.  
    Also, it depends on the direction that you are taking. You could use the Ray given in ScreenPointToRay as part of a Raycast. Thus getting the point on the colliders that the mouse hits.