How to move grabing object closer or further from view?

I have built into my game the ability to pick up an object with the mouse and move it around using the method in this video.
(https://www.youtube.com/watch?v=XVND_jo-kNo&t=2645s)

However, I would also like to be able to adjust the location of this picked up object to the near or far position relative to the camera, but I can’t seem to come up with a good blue print.
Can anyone have an idea on how to do this and give me some tips?

What’s the actual problem?
Do you know what input method to use? E g, maybe mouse wheel?

What I would do, would be to keep a “distance of picked up object” variable.
Moving the mouse wheel (or other input) would increment or decrement this, within some limit (e g, between 100 and 1000 units, or something like that)

Then, each frame, I would cast a ray forward through the camera and see if something collides, out to the maximum distance (1000) units.
Then, I would calculate the minimum of “desired distance” and “colliding distance,” and I would multiply the forward vector of the camera by this distance; this gives me the “desired midpoint of the object.”

Now, use whatever it is you do, to place/drag/move the object towards this point. (I don’t know which particular method you’re using, whether it’s physics or just placement, etc – whatever you do, use this determined position as the position)

You might want to back off a little bit if the ray cast finds a collision, btw – get the normal of the hit point (if there is a point) and offset the point out by the normal by something like 30 or 50 units. That way, there’s less aggressiv penetration between the grabbed object and some wall or rock or ground or whatever.

Hi @owowowsam

You should be able to do so as the object is moving along with the mouse cursor. If you move further away from the camera (up on the screen) your mouse position on the screen is adjusted to the position in the world.

This is the only code I use to move my buildings around the map. Green connection is a float for 1 000 000 to see from the camera to the landscape. The location you are getting out of this would be the location on the map where you move your object.

Let me know how it works.

Thank you for comments and hints. After I tried the distance between camera and the grab component. It is still not working smooth yet but I think I could get closer to what I wanted.


simply I use up and down key to change offset number to the distance and apply the new location.The object follows the mouse cursor immediately, so it cause some kind of jitteriness.

Let me know if anyone havs a better way to smoothen this movement…