L
L
LOKOFUN2020-01-14 12:28:17
3D
LOKOFUN, 2020-01-14 12:28:17

Object following the cursor?

Hello.
I need to move a single object, which I select by clicking, behind the mouse cursor. Now he is moving back and forth and up and down, I still need to do it left and right, but something doesn’t work out for me (((
Help, please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twobomb, 2020-01-14
@twobomb

Hang on the mouse movement handler the calculation of the angle between the mouse coordinates and, for example, the center of your object, remember this angle.
Further, at each iteration, we move to the object in this direction with the desired speed of the type

var speed = 10*deltaTime;
    objx += Math.cos(angle) * speed;
    objy += Math.sin(angle) * speed;

Well, in the end, you can also add the calculation of the distance between the last mouse coordinates and the current position of the object, and if it is less than speed, then we do objx = lastmousex, objy = lastmousey ... And check if the coordinates are equal, then do not move our object ..

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question