Search Unity

mouse/cursor control

Discussion in 'Scripting' started by morgansaysthis, Jun 11, 2007.

  1. morgansaysthis

    morgansaysthis

    Joined:
    Jan 12, 2007
    Posts:
    245
    im making a 2D side scrolling game and im running into some problems with the mouse


    1. how do i use the
    Code (csharp):
    1.  Input.mouse.position
    coordinates to have an object point at the mouse on the z-y plane


    2. how do i force the cursor to a position on screen?? (like moving it to the center of the screen every-time you loose a life)


    3.and how do i control the scroll wheel??
     
  2. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    To point a 3D object at the mouse, do this:

    Code (csharp):
    1.  
    2. var distanceFromCameraToPlayer = 0.00; // the distance in between the camera and the player in 3d space.
    3. var objectToAim : GameObject; // our gun, I assume
    4.  
    5. function Update ()
    6. {
    7.     aimPoint = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, distanceFromCameraToPlayer / Camera.main.farClipPlane)); // the point to aim at. It is on the same plane as the player and follows the mouse.
    8.  
    9.     objectToAim.transform.LookAt(aimPoint); // aim the object.
    10. }
    11.  
    I am not sure you can move the cursor around. But Screen.lockCursor = true; yield; Screen.lockCursor = false; might center it.

    To set up an input axis for the scroll wheel, go to the input manager and set an axis to: Type = Mouse Movement, Axis = 3rd Axis (Joysticks and Scroll Wheel).
     
  3. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    The manual doesn't say it, but Input.mousePosition is read-only. If you want to move the cursor through scripting, you'll have to hide the real cursor and make your own using a GUITexture or something.
     
  4. morgansaysthis

    morgansaysthis

    Joined:
    Jan 12, 2007
    Posts:
    245
    yoggy i couldn't get that code to work, i put it in just how it was and then changed some things when it wasent working, i added the distance measurement because the zoom is always changing in my game so the camera distance is never the same, and i changed the .x mouse pos to .z ( although im not sure if i was supposed to) because left and right in my game is z


    anywho heres what i changed it too




    Code (csharp):
    1. var objectToAim : GameObject;
    2. var maincamera : Transform;
    3. // our gun, I assume
    4.  
    5. function Update ()
    6. {
    7.    
    8.    
    9. var distanceFromCameraToPlayer =Vector3.Distance(maincamera.position,transform.position);
    10.  
    11. aimPoint = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, distanceFromCameraToPlayer / Camera.main.farClipPlane));
    12.  
    13. objectToAim.transform.LookAt(aimPoint);
    14. }
    15.  
    right now the z axis points at the camera and the gun, which is a particle system, shoots away from the camera


    thanks for your help, this is pretty over my head
     
  5. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    Updated Script:

    Code (csharp):
    1.  
    2. // place this on the gun. The gun's z axis should point where it shoots and it's y axis should point at the camera
    3.  
    4. function Update ()
    5. {
    6.     screenMousePosWithDepth = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.WorldToScreenPoint(transform.position).z);
    7.     aimPoint = Camera.main.ScreenToWorldPoint(screenMousePosWithDepth);
    8.     transform.rotation = Quaternion.LookRotation(aimPoint, Vector3.left);
    9. }
    10.  
    And a simple scene to explain it's use:
     

    Attached Files:

  6. morgansaysthis

    morgansaysthis

    Joined:
    Jan 12, 2007
    Posts:
    245
    sweet it works great, i really appreciate it dude