- Home /
Select 2D Object By Mouse ?
Hi ...
I'm trying to make a 2D Game but I have this problem ...
How can I select the character by clicking on it ?
Note :
I've 3 Layers ( Background , Characters , Objects )
In 3D game I can use ( ray , RaycastHit and Physics ) to do that , but I tried to convert it to 2D , but it doesn't work ...
Please how can I do that ?
Answer by Spinnernicholas · Dec 16, 2013 at 11:20 PM
This will return a 2D collider at the current mouse position. It works by calculating the 2D position of the mouse from the Orthographic camera. Then, doing a 2d raycast with a single point(zero length), but infinite depth(distance from camera). This works because Physics2D.Raycast() can return colliders that the ray begins inside.
Physics2D.Raycast(position, direction, length);
Here:
Physics2D.Raycast(new Vector2(camera.ScreenToWorldPoint(Input.mousePosition).x,camera.ScreenToWorldPoint(Input.mousePosition).y), Vector2.Zero, 0f);
[OLD]:
Don't forgot to use Physics2D.Raycast().
this is the full code ^_^
var hit: RaycastHit2D = Physics2D.Raycast(Vector2(camera.ScreenToWorldPoint(Input.mousePosition).x,camera.ScreenToWorldPoint(Input.mousePosition).y), Vector2.zero, 0);
thank you soooooooooooo much :)
Thank you, Spinnernicholas! this helped me.
Here is the c# full method:
//This method returns the game object that was clicked using Raycast 2D
GameObject ClickSelect()
{
//Converting $$anonymous$$ouse Pos to 2D (vector2) World Pos
Vector2 rayPos = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
RaycastHit2D hit=Physics2D.Raycast(rayPos, Vector2.zero, 0f);
if (hit)
{
Debug.Log(hit.transform.name);
return hit.transform.gameObject;
}
else return null;
}
I try this and that didn't work. I create a Debug.Ray but nothing appear. I also try to configure the $$anonymous$$i and max depth (0 and infinity) for the same result. Some idea ?
Answer by robertbu · Dec 16, 2013 at 11:45 PM
There are three ways for you to solve your problem:
1) Use as script with an OnMouseDown() function attached to each selectable object. OnMouse* functions work with both 2D and 3D colliders.
2) Use a 3D collider on a child object. You cannot have a 2D and a 3D collider on the same object, but you can have an empty game object as a child of your sprite with its own collider. You'll need to pick and size the collider as appropriate to your sprite. You can use the transform.parent of the child object if you need to get access to the sprite and/or its scripts. You would use the original Physics.Raycast().
3) Here is a hack I discovered yesterday. First convert your mouse position into a world coordinate using Camera.ScreenToWorldPoint(). Then use that position to do a Physics2D.Raycast() with a very short ray (like 0.05). It does matter what direction, but the distance needs to be short.
thank you . the first and second are a good solutions , but I need to select it by raycast this code I use for 3D , How can I convert it to 2D ?
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if(Physics.Raycast(ray,hit))
print(hit.collider.gameObject.name);
thanks ...
That's solution #2:
Add an empty child game object to your 2D object at the same position (i.e. local position (0,0,0).
Add a collider such as a box collider to the empty game object.
Size the collider to fit the 2D object
If you want to output the name of the 2D object, then the last line would be:
print(hit.transform.parent.name);
Answer by MFen · Dec 16, 2013 at 07:22 PM
pseudo code
if (Input.GetMouseButtonDown(0) && myPlayerRect.Contains(Input.mousePosition))
{
//I've clicked on the player
}
Answer by j_pablo_gn · Oct 01, 2016 at 04:27 PM
This works for me by attaching the script to the main camera (Orthographic). It is based on Spinnernicholas solution.
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
Vector2 origin = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
RaycastHit2D hit = Physics2D.Raycast(origin, Vector2.zero, 0f);
if (hit) {
print(hit.transform.gameObject.tag);
}
}
}
Your answer
Follow this Question
Related Questions
How do I use layermasks? 9 Answers
Lights that ignore position.z of GameObject 0 Answers
Lifting box 2D C# 0 Answers
2D (With Planes) Jump Script 0 Answers
How can I make a game object move in parabolic motion as if it were under gravity? 2 Answers