im making a 2D side scrolling game and im running into some problems with the mouse 1. how do i use the Code (csharp): 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??
To point a 3D object at the mouse, do this: Code (csharp): var distanceFromCameraToPlayer = 0.00; // the distance in between the camera and the player in 3d space. var objectToAim : GameObject; // our gun, I assume function Update () { 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. objectToAim.transform.LookAt(aimPoint); // aim the object. } 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).
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.
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): var objectToAim : GameObject; var maincamera : Transform; // our gun, I assume function Update () { var distanceFromCameraToPlayer =Vector3.Distance(maincamera.position,transform.position); aimPoint = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, distanceFromCameraToPlayer / Camera.main.farClipPlane)); objectToAim.transform.LookAt(aimPoint); } 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
Updated Script: Code (csharp): // 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 function Update () { screenMousePosWithDepth = Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.WorldToScreenPoint(transform.position).z); aimPoint = Camera.main.ScreenToWorldPoint(screenMousePosWithDepth); transform.rotation = Quaternion.LookRotation(aimPoint, Vector3.left); } And a simple scene to explain it's use: