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

Spawn on mouse location

Discussion in 'Scripting' started by Persona, Oct 22, 2011.

  1. Persona

    Persona

    Joined:
    Jul 25, 2010
    Posts:
    280
    I'm trying to make a script for spawning an item within a radius around where the mouse is positioned on screen, like in the example below. I've made a script that works for overhead viewpoint, but on third person view, behind the shoulders, it is off by a wide margin when it comes to depth perception. So can anyone:

    * Show me how to correct it
    * Draw the radius or point on screen so the player will know where it will be spawned

    My Code so far:
    Code (csharp):
    1.  
    2. if(Input.GetMouseButtonDown(0)){
    3.  
    4.  var playerPlane = new Plane(Vector3.up, transform.position);
    5.         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    6.         var hitdist = 0.0;
    7.        
    8.         if (playerPlane.Raycast (ray, hitdist)) {
    9.             var targetPoint = ray.GetPoint(hitdist);
    10.             }
    11. Spawn();
    12.     }
    13. }
    14.  
    15.  
    Example: http://youtu.be/M0EdKtq9fUo?t=1m12s
     
  2. Persona

    Persona

    Joined:
    Jul 25, 2010
    Posts:
    280
    Bumping