Hello, I have a sprite gameobject which is a ball, I want to detect when the player clicks on the ball. I have tried Code (csharp): RaycastHit hitInfo = new RaycastHit(); if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo)) { Debug.Log( hitInfo.transform.gameObject.name ); } but the ball does not get detected at all.. I have added 3D objects to my scene to test it and it works for 3D objects fine.. Does anyone have any idea on what's wrong? Thanks.
Hey all i changed "Physics.Raycast" to "Physics2D.Raycast" and "RaycastHit" to "RaycastHit2D" Like that: Code (csharp): RaycastHit2D hitInfo = new RaycastHit2D(); if (Physics2D.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo)){ Debug.Log( hitInfo.transform.gameObject.name ); } but i get these errors: "error CS1502: The best overloaded method match for `UnityEngine.Physics2D.Raycast(UnityEngine.Vector2, UnityEngine.Vector2)' has some invalid arguments" and " Code (csharp): error CS1503: Argument `#1' cannot convert `UnityEngine.Ray' expression to type `UnityEngine.Vector2'" Where do i make mistake?
You need to pass a Vector2 as the first argument, not a Ray, the second argument should be a Vector2, and the RayCastHit2D is returned by the function. Looks like you've just got a bit confused between the Physics.Raycast and Physics2D.Raycast syntax. Try this: Code (csharp): RaycastHit2D hitInfo = Physics2D.Raycast(mouseWorldPosition, Vector2.zero); if(hitInfo != null) { Debug.Log( hitInfo.rigidbody.gameObject.name ); }
Not working.... In JS gives this error: BCE0023: No appropriate version of 'UnityEngine.Physics2D.Raycast' for the argument list '(UnityEngine.Vector3, UnityEngine.RaycastHit2D)' was found.
Sorry, my bad, wrote that reply without actually compiling it - I've amended my original answer with the correct C# script. For Javascript, try: Code (csharp): if (Input.GetMouseButtonDown(1)) { var hit : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); if(hit.collider != null) { Debug.Log("object clicked: "+hit.collider.tag); } } This is basically 2D ray casting with a vector of size zero, which will collide with the object immediately underneath the cursor position.
For some reason it is still doing an error. Here is the full code Code (csharp): #pragma strict function Start () { } function Update () { if (Input.GetMouseButtonDown(1)) { var hit : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); if(hit != null) { Debug.Log("object clicked: "+hit.collider.tag); } } } This is making :NullReferenceException: Object reference not set to an instance of an object Global.Update () (at Assets/Scripts/Global.js : 8 ) I have tried to make the camera public like this Changed the hit with And drag and drop the camera in the public slot, but got the same error Is it only me? Why this code would make an error?
Oups working! I made a mistake in the last post. Here is the working script, hopefully it will help someone! (not forget to drag and drop the active camera in the camera public slot of the script.) Code (csharp): #pragma strict public var cam:Camera; function Start () { } function Update () { if (Input.GetMouseButtonDown(1)) { var hit : RaycastHit2D = Physics2D.Raycast(cam.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); if(hit != null) { Debug.Log("object clicked: "+hit.collider.tag); } } }
Code (csharp): #pragma strict function Start () { } function Update () { if (Input.GetMouseButtonDown(1)) { var hit : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); if(hit.collider != null) { Debug.Log("object clicked: "+hit.collider.tag); } } } Seems that hit the 'hit' variable returned by the function is always valid, but you need to check that the collider is non-null *edit* And you need a valid main camera, of course, as noted above
RaycastHit2D is a struct therefore you won't get a NULL returned but as mentioned the collider will be valid or NULL. However, to save you from having to perform the ugly explicit check whether the collider is null or not, RaycastHit2D has an implicit conversion operator to bool so you can do: Code (csharp): if (hit) { ... }
hello, i seem to have the same problem. I tried to copy this solutions but i doesn't work for some reason. Code (CSharp): void Update () { if (m_turn == TurnState.PlayerInput) { if (Input.GetButtonDown("Fire1")) { Debug.Log(m_ray.OnMouseClick2D()); } } } public RaycastHit2D m_hit2D; public GameObject OnMouseClick2D(params int[] _mask) { //m_ray = Camera.main.ScreenPointToRay(Input.mousePosition); foreach (int mask in _mask) { m_hit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition) , Vector2.zero, 1000.0f, 1 << mask); if (m_hit2D.collider != null) { return m_hit2D.transform.gameObject; } } m_hit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition) , Vector2.zero); if (m_hit2D.collider != null) { return m_hit2D.transform.gameObject; } return null; } My scene is full of Sprites, but i allways get null. Some guesses?
i have this script and didn't find any error on it, but when i play it the door wasn't open, please help me
This appears to work with orthographic cameras but not perspective. Seems like 2D Raycasts using ScreenToWorldPoint don't support perspective cameras! Try GetRayIntersection and ScreenPointToRay instead. Code (CSharp): RaycastHit2D rayHit = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(Input.mousePosition));