- Home /
How do I get mouse position in game units?
So I'm creating a game for smart phones and eventually is going to be so that there is one cannon on the top part of the screen and when I put my finger anywhere on the screen it will shoot a ball towards my finger. But I want to test it on the computer so I tried to do it like this:
if (Input.GetMouseButtonDown(0)) {
Instantiate (prefab, new Vector2(Input.mousePosition.x, (Input.mousePosition.y, Quaternion.identity);
}
But the problem is that Input.mousePosition is in pixels. Even better would be if there was an object always at the same place as the mouse in game.
So hopefully that is clear enough. And sorry for bad English it's not my native.
-Michael
Answer by belvita · Dec 03, 2013 at 01:05 PM
you mean u want to get the world coordinates?
if yes then:
GameObject x=Instantiate (prefab, new Vector2(Input.mousePosition.x, Input.mousePosition.y, Quaternion.identity);
public Transform target; void Update() { Vector3 screenPos = camera.WorldToScreenPoint(x.transform.position);
now ur gameobject has the coordinates in screen position
I got your code working some how it had some problems but still it just spawns them every where.
Answer by Vardan Meliksetyan · Dec 03, 2013 at 02:39 PM
Hi you can use Camera class http://docs.unity3d.com/Documentation/ScriptReference/Camera.html
You can look Event.current.mousePosition http://docs.unity3d.com/Documentation/ScriptReference/Event.html
Answer by Soldeplata · Jan 08, 2016 at 08:17 PM
This is your answer:
http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html
Your answer
Follow this Question
Related Questions
Check mouse click in front of object 0 Answers
question about instantiating 2 Answers
Get the color of a pixel at mouse position? 2 Answers
How I can save mouse position ? 0 Answers
Mouse plane does not detect height 1 Answer