Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Particle system to mouse?

Discussion in 'Scripting' started by BlitheD, Sep 27, 2006.

  1. BlitheD

    BlitheD

    Joined:
    Aug 28, 2006
    Posts:
    36
    Is there a way to attach a particle system to the mouse so that it follows the cursor across the screen?
     
  2. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    You could get the mouse postion, and then use that to determine where to move the particle system. Something like:
    Code (csharp):
    1.  
    2. function Update () {
    3.     // Get the mouse position in pixels, and convert to camera view by dividing by the number of pixels the camera is displaying.
    4.     x = Input.mousePosition.x / mainCamera.pixelWidth;
    5.     y = Input.mousePosition.y / mainCamera.pixelHeight;
    6.     transform.position = Vector3(x, y, 0);
    7. }
    8.  
     
  3. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    This one will be relative to the active camera.

    Code (csharp):
    1.  
    2. function Update ()
    3. {
    4.   var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    5.   // 10 meters in front of the camera:
    6.   transform.position = ray.GetPoint(10) ;
    7. }
    8.  
    (Note i havent tried it out, so there might bea typo in the script)
     
  4. BlitheD

    BlitheD

    Joined:
    Aug 28, 2006
    Posts:
    36
    Hi. Thanks for your help. I'm not sure why but with Ifrog's script, Unity tells me "mainCamera" is an unknown identifier. I'm not sure what the fix for that is. Joachim's script worked but "ScreenSpacePointToRay" is actually "ScreenPointToRay" - in case anyone else would like to try it out. It looks like that will do the trick but I need to play with the particles now to get them to look the way I want. Thanks again, guys.
     
  5. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    You are getting error on mainCamera, because there is no variable defined called mainCamera.

    The correct name for the camera tagged as the main camera is Camera.main.
     
  6. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    I like Joachim's script :)