Hello, I need a code that make a object (ex: sphere) stay at mouse position in X and Y axis and at 0 position in Z axis. I need this to make my Gun in 2.5D sidescroller shooter, the gun is already facing the object, I just need to make it stay at crosshair position. Already tried ScreenToWorldPoint, but no sucess... ;/
You were on the right track, ScreenToWorldPoint is what you need. Code (csharp): // totally untested made-up-on-the-spot code... Vector3 gunPosition = Input.mousePosition; Camera cam = Camera.mainCamera; gunPosition.z = -cam.position.z; gunPosition = cam.ScreenToWorldPoint(gunPosition); myGunObject.transform.position = gunPosition;
Thanks Cameron, It worked almost perfectly, with some adjustments its now this way: Code (csharp): function Update () { gunPosition = Input.mousePosition; gunPosition.z = -Camera.mainCamera.transform.position.z; gunPosition = Camera.mainCamera.ScreenToWorldPoint(gunPosition); transform.position = gunPosition; transform.position.z = 0; And the arm script is Code (csharp): var fpc : GameObject; function Update () { transform.LookAt(fpc.transform.position); }