5

When the mouse is at the Door (red area) i want to do something. I am trying to cast a Ray but the Ray doesn't hit the Door and i cant find where exactly it is hitting. Also how can i Debug.DrawRay this ray?

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;

if (Physics.Raycast(ray, out hit, Mathf.Infinity)) 
{
    if (hit.collider.tag == "InteractiveDoor")
    {
        doorInteractGameObject.SetActive(true);
    }
    else
    {
        doorInteractGameObject.SetActive(false);
    }
}

Unity Img

spaleet
  • 838
  • 2
  • 10
  • 23
Gerte
  • 97
  • 2
  • 4
  • 9
  • "It doesn't work" is not a specific question. However, minimal reproducible source code provided, that's a plus! Please be more specific what "doesn't work". Are there errors in the console? What is the expected behaviour, what happens instead? Edit your question and clearify. – Pinke Helga May 26 '18 at 23:43
  • There are no error, the raycast works but it doesn't hit where i want it to hit. I am trying to `debug.drawray` to see exactly where it is hitting but i cant find how to do that since it wants Vector3 and `Camera.main.ScreenPointToRay(Input.mousePosition)` is a Ray. – Gerte May 26 '18 at 23:47
  • 1
    Are there colliders on the targets? – Pinke Helga May 26 '18 at 23:48
  • 2
    Oh my god i am stupid :) I have a Sphere Collider on the parent of the 9 boxes and i somehow thought that's enough. I added box colliders to all 9 boxes and the code works fine. Thanks for help! – Gerte May 26 '18 at 23:56

3 Answers3

0

You can draw this ray as:

Debug.DrawRay(ray.origin, ray.direction);

or

Debug.DrawRay(Camera.main.transform.position, Camera.main.ScreenPointToRay(Input.mousePosition).direction);

Option 1 is more direct once you've defined your ray, but option 2 gives you more choice to play around if it turns out this Ray doesn't behave the way you expect it to.

camera.ScreenPointToRay expects a Vector3 but Input.mousePosition returns a Vector2. The Unity docs on Vector3 and Vector2 appear that you should be able to use Vector2 as Vector3 implicitely and it'll populate the z component as 0, but if the Debug.DrawRay shows that the ray is the issue, then you need to append a 0. Maybe something like:

Ray ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
jkidd
  • 26
  • 4
  • 1
    The first one works but it's not 100% accurate to the real Ray, the second one give me error because `Debug.DrawRay` takes Vector3 and `Camera.main.ScreenPointToRay` is a Ray – Gerte May 27 '18 at 00:04
  • Good catch on the second one! I've updated my response so that it returns the Vector3 for the ray direction. If the first one isn't drawing the ray you are expecting, then you may not be building the ray you intend to since the first approach is calling the ray directly. The second approach's origin is my guess at where that ray should be starting, based on `ScreenPointToRay` being a method of a camera object. It does sound like you got this working based on your comment to the original question though, so everything is hopefully working now. Does it behave when the mouse is at the door edge? – jkidd May 27 '18 at 00:38
  • 1
    Yeah it works fine as long as the mouse is located at any of the 9 boxes i added the colliders and the tag. – Gerte May 27 '18 at 01:05
0

You might need to create a LayerMask and add it to the parameters for Physics.Raycast; also make sure that the door has a collider on it. You would be surprised how many times I just forgot to add a collider.

In terms of drawing the ray in case this is not working for some reason. Use Debug.DrawRay. I recommend putting it in FixedUpdate() or Update(). Color and duration you can set to whatever you want but I recommend having depthTest be false since you want the ray to be drawn regardless. Then call it like:

Debug.DrawRay(ray.origin, ray.direction, <color>, <duration>, false);
0

The fix is to go to your camera and set the tag to 'MainCamera'. Or you can modify your code to have a camera variable and use that variable instead of Camera.main.

Gurbela
  • 1,184
  • 2
  • 16
  • 40