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()
.
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.
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.
@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?
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
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.
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:
-
Move your entity in the direction of the mouse when you press the W key.
(Using the mouse to determine the forward direction). -
Move the entity always to the top when you press the W key.
(Using the mouse to look arround only).
Yes, I want this one.
Can I see your fork?
Handled internally, topic solved.