- Home /
Can Camera.WorldToScreenPoint be used to check if one collider is over another in screen space?
Hello unityAnswers its me again with another annoying question. This one may be more straightforward.
Is it possible to somehow use Camera.main.WorldToScreenPoint or ScreenToWorldPoint to check if two colliders are overlaping in screen space ?(I'm not looking for if they overlap in WorldSpace, that's easy anyway)
Im also open to other methods of checking Screen Space collision. Whatever works.
A more simple version of my question is How do I detect collision in ScreenSpace?
Well you are going to need more than points of course - you could convert the bounding points of the AABB to screen space and test a point in that. Can you describe more about what you are trying to do?
http://answers.unity3d.com/questions/380648/alternative-to-onmousedown.html
Here is my past question and the same problem I am trying to work out. If I can tell if one collider is overlapping another in screen space than my problem may be solved.
Answer by robertbu · Jan 15, 2013 at 04:24 PM
Raycast is the way to solve your original problem. Here is a test test script.
using UnityEngine;
using System.Collections;
public class RandomObjects : MonoBehaviour {
public GameObject goGenerate; // Game object to generate
public Camera cam; // Camera to use for the raycast
public GameObject goCrosshairs;
Plane plane;
void Start ()
{
plane = new Plane(Vector3.forward, new Vector3(0,0,cam.gameObject.transform.position.z + 0.5f));
for (int i = 0; i < 35; i++) // Generate some random objects
{
Vector3 v3Pos = Random.insideUnitSphere * 5.0f;
v3Pos.x *= 2.0f;
Instantiate (goGenerate, v3Pos, Quaternion.identity);
}
}
void Update ()
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
float fDist = 0.0f;
RaycastHit hit;
plane.Raycast (ray, out fDist);
goCrosshairs.transform.position = ray.GetPoint (fDist);
if (Input.GetMouseButtonDown (0))
{
if (Physics.Raycast(ray, out hit))
{
Destroy (hit.collider.gameObject);
}
}
}
}
Ok Im gonna try this code but I raycasted before and it didnt work for me. Will this code work if the crosshair is not near the center of the screen?
To use the above script: 1) Attach this script to an empty game object; 2) create a game object or a prefab to use as the cross hairs and in the inspector, remove the collider, and drag it on top of the goCroshairs variable; 3) create a game object or prefab to use as a basis for the random objects in the scene and drag it in the inspector on top of the goGenerate variable; 4) drag the main camera on top of the cam variable. Hit play.
Note the crosshair game object is displayed on a plane close to the camera, so it will have to of small size. The Plane is an internal construct not a game object. It is used to calculate where to put the crosshairs.
Note I wanted a working example for you to show that Raycast does work. After playing with this script a bit, you might want to go back to your original script and figure out why it did not work. $$anonymous$$aybe you had a collider on your crosshairs, or you did not create the Ray the right way, or your scene objects did not have the collider setup correctly, or...
Wow it works better than ever before thanks! Is there a way to get it to work in the entire crosshair and not just the 1pixel center point?
$$anonymous$$aybe I would have to cast multiple rays to achieve this? Something like?