How do i snap the end point of a Raycast to a certain transform

Hi all

I’m making a VR project and I’m using a Raycast as a laser from the player’s hand for moving agent’s from one place to another.

What I want to happen is that when the laser is cast and the endpoint hits an oversized collider of the object, I make the endpoint of the laser snap to that object.

Once I’ve figured a simple way of getting this working with a single object, I’ll be able to move on and use multiple objects.

Here’s what I have so far. The part I’m not sure of is just to make the end of the Raycast snap to the centre of the object.


        if (Physics.Raycast(laserPosition.transform.position, laserPosition.transform.forward, out hit))
            if (hit.collider == waypoint)
                      // ????????????

I’m not sure I completely understand what your asking , so you might need to clarify if I’m misunderstanding what your looking for. However it sounds like you are just looking to make a ray cast travel to a specific object. The simple solution to this is just to make a line cast between the ray cast origin and hit.transform.position once the raycast is triggered. However if you still want to use a raycast then you would just need to calculate the distance and direction between the raycast orgin and hit.transform.position. The distance would just be the orgin point subtracted from transform position, and the direction would just be the distance normalized.