# How To Place Object On Ground Within Radius?

I'm using Unity and I'm building an object placement system where you'll be able to position objects around the player.

I'd like to place objects flush with the ground, parallel to world up, even on slopes, so I'll use a raycast for that.

How would I limit the distance to maxDistance? I tried using ScreenPointToRay but the max distance on the raycast makes the movement of the object stop if the raycast doesn't hit.

As an example, If I'm looking down in front of me, the object i'm controlling should be at that position. However, if I'm looking straight forward into Infinity, the object should only ever be maxDistance away. If I rotate, the object should follow smoothly.

Thanks!

edit: here is a video of what I have now, but as you can see, the object gets stuck if the raycast doesn't hit anything - due to the maxDistance. It doesn't get stuck if i use Infinity for the ray distance, but then the object doesn't respect max distance like I want it to. https://streamable.com/bacij8

• Can you show us the viewpoint you're using? – DMGregory May 31 at 18:02
• Sure. I've edited my original post with a link to a video. – Blixkreeg Jun 1 at 7:22