1
\$\begingroup\$

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

Code snippet: https://gist.github.com/zackperdue/270e9574bed4a937bef791fb41512291

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.