- Home /
Find Mouse co-ordinates on click in a 2d environment
Basically I am attempting to find the x,y coordinates of the mouse cursor when clicking the left mouse button, the reason for this is I have a character with moves along the 2d plane and I want to be able to fire projectiles toward the cursor originating from the character model.
As far as I know the best option is to use ray trace but im not 100% sure how to go about doing so.
Thanks in advance
Answer by darthbator · Aug 04, 2012 at 10:43 PM
You're going to want to make sure that both the actorObject's coordinates and the mouse coords are occupying the same coordinate space. Input.mousePosition returns screen coordinates and your game actors position will be in world coordinates. So I imagine you're going to want to either do a raycast against a plane established from your character with Camera.ScreenPointToRay or you're just going to want to translate the screen coordinates to world coords with Camera.ScreenToWorldPoint. I'm not entirely sure what your game looks like so I can't really say what will give you the coordinates you want as lots of people say they are making a "2d" game but are actually using all 3 dimension of depth inside the engine and just restricting character and object motion to two axes.
Once you're gotten the cursors position in world space coordinates you should be able originate objects from the transform of the players character object and throw them and the location of the cursor.
Answer by williampigmeu · Aug 04, 2012 at 05:57 PM
Find X and Y mouse coordinates, calculate the distance between the character that will shot and the mouse, instantiate the projectile, use deltaTime to make it move smoothly to the distance vector.
only use vector2 as well makes life easier for 2d as you don't use z much.
Hi, thanks for the answer, I had an idea it was along those lines however I'm quite new to scripting so am unsure of how to go about it.
I'm not asking for the complete code but if you could point me in the right direction (links to tuts or docs that could help?) that would be awesome.
Thanks again
Answer by williampigmeu · Aug 04, 2012 at 06:51 PM
For the mouse, have a look at Input.mousePosition. To calculate the distance use a Vector2, something like "distance = Vector2(transform.position, Input.mousePosition);".
Input.mousePosition: http://docs.unity3d.com/Documentation/ScriptReference/Input-mousePosition.html
Also, look at that question.
http://answers.unity3d.com/questions/50953/2d-sidescroller-crosshair.html
I think I have managed to get my head round the theory behind the code but I am really struggling with actually putting it into action. Is there any chance you could provide a very simple bit of code I could work off of? thank you again.
Answer by williampigmeu · Aug 04, 2012 at 10:29 PM
Here is it:
var distance; var smooth: float = 0.5
function Update() { if (Input.GetButtonDown("Fire1")) { distance = Vector2(transform.position, Input.mousePosition);
transform.Translate(distance, smooth);
}
}
Or something like that, this is only an example, it doesn't really work.
Obs.: In this example, it is supposed to be attached to the bulled, and the bullet to be in "back" (Z coord) of the weapon, like inside the weapon, but, I said, only an example.
Thanks again for the help, unfortunately I just cant seem to master it so my project will have to do without for now haha.
Do something else, watch some tutorials do some simple stuff, thats what i did and then when i went back to someyhing i did not know, it became easier to grasp.
Answer by John_South · Jul 08, 2014 at 10:27 AM
if (Input.GetMouseButtonDown(0)) {
var pos = Input.mousePosition; pos = Camera.main.ScreenToWorldPoint(pos);
var q = Quaternion.FromToRotation(Vector3.up, pos - transform.position);
Rigidbody2D go = Instantiate(bulletPrefab, transform.position, q) as Rigidbody2D;
go.rigidbody2D.AddForce(go.transform.up * 500);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613075155im_/https://answers.unity.com/themes/thub/images/avi.jpg)