How to constrain the movement of an object

※I use a translator because I live abroad. If the translation is strange, please let me know!

I created a system that allows you to place objects on a table using the object dragging system. However, we are trying to create a system that prevents objects from leaving the table, but a problem occurred in the process.

스크린샷 2023-08-13 211453
스크린샷 2023-08-13 211418

Originally, I tried to use “Touched” to forcibly move the object’s position when it does not touch the table on the back, but since “Touched” did not work when the object was anchored, I can’t had to use that method.

I don’t know how to do it. That’s the limit of my imagination. Can you give me an idea how to implement this? If such a question violates the regulations, it will be deleted immediately. I’m really sorry and thank you…

2 Likes

You can cast a ray from mouse. And if it hits the table, move the object, if it doesn’t don’t move the object.

If you don’t know how to raycast from mouse, you can read this:

2 Likes

Thank you so much!!! I’ll try and give you an answer.

1 Like

I’ve tried many things… but that doesn’t work. Are there any other ways? I’m really sorry.

I don’t think there are other ways. (And if there are other ways, they are probably harder to do than raycasting) So I think the easiest way is to raycast. And if you don’t know how to raycast, there are so many tutorials on devforum (don’t watch youtube tutorials about raycasting because most of them are outdated), it takes some time to learn but when you learn it, it becomes easy.

1 Like

thank you I’ll have to try it again!

I have one last question. Can you tell me where should I put that script? This was the problem I was referring to in my previous post. I don’t know if it’s a server script service, a starter gui, or a starter player. I’m really sorry, but I’d really appreciate it if you let me know.

It should be a local script because you can only get the position of the mouse from client-side.

(But you can make a server-sided anti-cheat if you want)

If so, can I just put the local script in “StarterPlayerScripts” or “StarterGui” folder?

Yes, you can put it to anywhere that local scripts can run

1 Like

Thank you very much! It runs really well. thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.