Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Display Projector only at the hit point of a raycast...?

Discussion in 'Scripting' started by LeftyTwoGuns, Jul 23, 2014.

  1. LeftyTwoGuns

    LeftyTwoGuns

    Joined:
    Jan 3, 2013
    Posts:
    260
    I'd like to use a Projector for an aiming reticle in the game world but the problem is Projectors pass through game objects.

    It's easy to keep a line renderer from passing through objects, because it has a beginning and end point you can reference, so you just tell the second point of the line renderer to only display wherever the raycast hitpoint is. But I'm clueless as to how you'd do that with a Projector. Is this possible?
     
  2. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    I'm not sure I follow what you mean.

    The Projector class has farClipPlane which specifies the max distance. You could set that according to a raycast distance plus a little extra to make sure. It might still get cut off if it's a very shallow angle, or project through the other side if it's thinner than the little extra.
    http://docs.unity3d.com/ScriptReference/Projector.html
     
  3. LeftyTwoGuns

    LeftyTwoGuns

    Joined:
    Jan 3, 2013
    Posts:
    260
    A Projector will go through all objects in the world space. So, if I aim a crosshair Projector on a cube, it will hit the cube but go through it and project on the wall behind it as well and then all objects behind that, etc. I want it only to project on the first surface it hits.

    Would setting the farClipPlane to a raycast hit point do that?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Yes, that's what farClipPlane does.
     
  5. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    farClipPlane will set a maximum distance for the Projector. So as long as you can get the distance you want to set, it should do the job.
     
  6. LeftyTwoGuns

    LeftyTwoGuns

    Joined:
    Jan 3, 2013
    Posts:
    260
    Yup, setting the farClipPlane as the hit.distance does the job. Thank you both!
     
  7. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    The stuff I was unsure about was edge cases where you're projecting on a very shallow angle to the surface, it will be stretched out. Now I just realized, depending on the look you're trying for, you could reposition the projector and rotate it according to the normal of the surface where the Raycast hit, and then it wouldn't stretch out.