[SOLVED] Entity look at mouse on y axis in 2D

Hello there! I need some help with this problem, I have no idea how to code so I rely on you guys to help. I need the player to look at your mouse to make the game a bit more like a .io game. Can someone help me? Also Can you help make a script where if playerAttack on a rock it gives them rock?

First you need to get the position of the mouse. Please check the onMouseMove event on the page below. Then you can use lookAt() to rotate the entity in the direction of the mouse position. Probably you also need rotate the entity 180 degrees after using lookAt().

https://developer.playcanvas.com/en/tutorials/mouse-input/

1 Like

Can you give me a straight script of your example? I need the “body” entity to look at the mouse.

If you want to make a game, you have to try it yourself first. Otherwise you’ll never learn.

2 Likes

Yes, but I have a limited time. Time is of the essence and not irrelevant.

I have tried learning as well as taking online classes, but all have failed due to the fact that playcanvas HTML CSS, and javascript are very different from what I have learned.

That goes for everyone here, including myself. If you want to make a game you need time. A lot of time.

The forum is meant for getting help if you get stuck. Create a script, add things I suggested and check the result. Error? Try to fix the error. Can’t get the right result? Share your script so others can check what could be wrong.

2 Likes

@Albertos You have helped me several times and I am thankful for that, I just would like to learn to code javascript for playcanvas that works. How would I do that?

I see you use a dynamic rigidbody. That means you can’t use lookAt() directly. You need to create a work around. First make the body model as a child, just like you did with the hands. With this we can use lookAt() on the body and apply this rotation later to player itself.

I also see you have multiple camera’s in your project. Can you explain which camera is used for what?

1 Like

I found an example, but I was unable to use it with your current setup. I will give it another try when I have some more time.

https://playcanvas.com/project/808772/overview/look-at-with-physics

2 Likes

The camera attached to the player is for the user(s) view. The second one is used for the mainscreen to see the server before you join.

1 Like

So when I run the script in my editor here’s the error I get:

[move-look-at.js?id=69351456&branchId=7e5d4afd-15d3-49af-b083-0f872753849e:78]: Cannot read properties of undefined (reading ‘applyForce’)

TypeError: Cannot read properties of undefined (reading ‘applyForce’)
at scriptType.MoveLookAt.update (https://launch.playcanvas.com/api/assets/files/scripts/move-look-at.js?id=69351456&branchId=7e5d4afd-15d3-49af-b083-0f872753849e:78:31)
at ScriptComponent._scriptMethod (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.51.7:64110:19)
at ScriptComponent._onUpdate (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.51.7:64155:11)
at ScriptComponentSystem._callComponentMethod (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.51.7:64720:49)
at ScriptComponentSystem._onUpdate (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.51.7:64737:9)
at ComponentSystemRegistry.fire (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.51.7:758:18)
at Application.update (https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.51.7:71265:17)
at https://launch.playcanvas.com/editor/scene/js/engine/playcanvas.dbg.js?version=1.51.7:71875:16

I don’t need the script to make the body move just look at the mouse (I already have a movement script)

Because you’ve attached the script to an entity that doesn’t have a rigidbody and you’re still trying to apply a force to the rigidbody. I expect that if you disable line 78 the error will be gone.

I have this working in a fork of your project. The rotation is also visible for the other players. I only rotate the model and not the rigidbody itself, because if I do that the camera will also rotate sinds this is a child entity. (This doensn’t look good and doesn’t play well).

I have two options for you:

  1. Move your entity in the direction of the mouse when you press the W key.
    (Using the mouse to determine the forward direction).

  2. Move the entity always to the top when you press the W key.
    (Using the mouse to look arround only).

1 Like

Yes, I want this one.

Can I see your fork?

Hey @Albertos Are you still commenting on this?

Handled internally, topic solved.