Moving object towards the mouse position

There is a 3D desk. I need to make a white sphere connected with the cursor movement, so it can “slide” on the desk’s surface (here height (y) is constant). It’s okay to get the mouse position, but I can’t translate it to world coordinates normally. I think that ScreenToWorldPoint function is the key, but I’m not sure.
Also, I don’t understand how to avoid the drop of cursor position (and therefore the object itself) through the desk. Maybe, it can be solved through raycast (it can find the object on the way) or sth like that? Sorry, if my thoughts are absurd, I’m a newbie in Unity.

Focus on ONE thing at a time. You’ve got about six different things in flight.

Learn how to cast from the mouse into the scene and find a world position that you hit. Just for that one simple task there are several parts involved: the object, the mouse, the camera, the colliders, etc.

Start from tutorials for ONE tiny thing. If you can’t get it from a tutorial, you certainly won’t get it from this tiny little text box.

Get familiar with each tiny part, live it, love it, do not move on until you understand 100% of what you are doing with casting into the scene to get a world position.

After you have the object snapping to position, start looking for tutorials on moving things toward other things. It’s everywhere in gamedev.

Two steps to tutorials and / or example code:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step to understand what is going on.

If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.

Imphenzia: How Did I Learn To Make Games:

Do you mean that making game is too early for me and I should learn more before?
If so - when I will be able (or when I’ll understand) to make a game, that I want?

I did not write anything like that above. If that’s what you took away, you might want to grab some coffee and settle in and re-read what I’m suggesting above. What I wrote is geared towards how normal humans learn things, which is to say one thing at a time.

NOTE: you may need to repeatedly go back over a concept before you grasp it. This is normal.

You will know this when you have successfully mastered all the parts necessary for whatever game that you want.

Well, I’m grateful to you for that advice. It’s helpful, but I’m not sure how to find tutorials that are close with my problem. Maybe, because I can’t divide my “goal” to elements to learn and connect at the end. That’s the reason I’ve tried to found the help here. So, I think that I can go reversely: see the full answer, divide it and understand, instead of finding the parts and go to the full answer. Isn’t it easier way here?

I’ve suggested a path already above. Start from casting a ray into the scene from the mouse.

There’s hundreds of tutorials out there for that. Do them until you understand every step of the way, all the parts and how they feed into each other. If you are not going to do that then you don’t really have anything to build upon to reason about the rest of the problem you’re trying to solve.

These problems really don’t get solved all at once. If you hire someone to do it, from your perspective it all gets solved at once, but that’s an illusion because they did it step by step. It’s like going to buy a car: That car just didn’t suddenly appear at the dealer complete and intact. It was built part by part by part. Software (and really anything complex) works the same way.

Go!! Do it! Shine! Throw yourself at it! NO MORE EXCUSES! It has never been easier to learn gamedev than today.

1 Like