[SOLVED] Rotate player to mouse position

no

I see my problem now.

onMouseMove only works whenever my cursor is moving. So when it is not moving, the last position of the mouse is used for my code.

Can I fetch the data of the mouse in the update function, so it will continuously get new coordinates from it?

Why is this causing a problem? What do you do in your code?

If your mouse is not moving you can’t get new data as far as I know. You only can use old data.

Ah, I think understand te problem already, but I’m curious why this doesn’t happen in the example project? I need to think about the solution.

There is no actual problem, it is how i described it:

onMouseMove only works whenever my cursor is moving. So when it is not moving, the last position of the mouse is used for my code.

For static cameras, this is no problem because if you don’t move the mouse, its last position is where it is on the screen.
For moving cameras on the other hand, if onMouseMove is not activated, the last position after the movement is taken as the coordinates in the scene.

The example project has a static camera. Therefore, there are no “problems”.

Since my camera is supposed to move with the player, I would want to check the position of the mouse every frame and not whenever it moves.

I just checked your project and I think you can solve this by creating a look at point that is a child entity of the player entity. You update the position of this entity with the mouse position.

How would that be in code?

Also, couldn’t I just get the mouse coordinates in the update function? That would be more efficient than having an extra object in the scene.

If the mouse does not send new coordinates, you cannot get new coordinates.

You can create an entity by script and add it as a child of your player.

That’s why I want to get the coordinates in the update function

The mouse only sends coordinates on events like mouseDown, mouseUp and mouseMove.

This is bad.

Anyway, how do I implement the second option?

Create an entity that is a child of your player enity. The position of the entity should be the lookAt position for your player entity. On your mouseMove event you update the (local) position of this entity.

Can you please help me with my code? I dunno how to get it working

I see I need to change my suggestion a bit. I’m working on an example.

Is this the right result?

1 Like

Yes it is

Could you send me a link to your fork please?

https://playcanvas.com/project/935579/overview/minigamejamfork

1 Like

Uuuh…my camera is getting weird.

for some reason, the camera is moving, even though is is not supposed to happen

Nevermind, I got it working fine. Thanks a lot to you

1 Like