Point and click code - bits and pieces

Hi all,

This might be a big ask, but I was hoping someone could walk me through the point and click code which I forked here and added some comments as well:

penguin-move | Editor (playcanvas.com)

Questions include:

1.) How can I type in the location the model needs to go via script(i.e. x=20,y=10.z=5)?
2.) Is there a way to bypass mousedown so the model will immediately go to the typed in location instead of waiting for an event?
3.) At the absence of a mousedown, is there a way for the script to fire with new location coordinates?

I tried coding directly this.targetposition.x , y, and z under movePlayerTo, which makes it move to a location, but I can’t seem to ask it to move more than once even through a function.

I have been playing with the script for a few days and can’t seem to make these 3 happen. This is also connected to my other question ( Making a model follow a character controlled by keyboard - Coding - PlayCanvas Discussion, but the project code there might be too crazy now and I figured I’d ask using a cleaner script.

Thank you again for being so accommodating to beginner users of Playcanvas. Any help would be greatly appreciated.

Quickly created an example for this where the movement is separated from the input: https://playcanvas.com/editor/scene/1375294

It shows ways two ways to approach this, event based with mouse-input.js and directly through the function keyboard-input.js

Thank you so much for this, Yaustar. This helps a lot connect a lot of the scripts together and helps me understand playcanvas better.

I tried putting this in my project (https://playcanvas.com/editor/scene/1373263) right out of the box to see how it will work. I’ve loaded the keyboard input script in spiderman_movement.js, loaded it in root and loaded Point and Click in player entity, just like the sample. Unfortunately, when I click key 1, I am getting an error how it “can’t copy read properties of undefined ( reading ‘copy’)” specifically on line 42 of point and click, which refers to movePlayerTo function when it copies the worldposition.

I noticed this doesn’t appear in your sample. Is this because the model where I loaded the script has a rigidbody? Maybe I should include teleport instead?

Thank you again, Yaustar.

Also, I managed to make spidey track the controlled player model, but he doesn’t actually go towards it. He only looks ate the player wherever he goes.

I checked the code for movePlayerTo and noticed how this.direction.sub2(this.targetPosition, this.entity.getPosition()); does work in getting the distance, but it doesn’t activate the if(this.distanceToTravel > 0) to make the entity(spiderman) walk. The entity also has speed so it should make the travel.

Here is the current movePlayerTo script in point-and-click.js:

The spiderman entity has a dynamic rigidbody. It has to be moved with forces/impulse/velocity etc. It cannot be moved by setPosition.

You can change it to be kinematic which will allow you to use setPosition but it will mean that it will go through other static rigidbodies.

The point of click script was not written to work with rigidbodies.

Thank you for this, Yaustar. Noted on the dynamic rigidbody.

As I was curious, I also copied the entity from the point and click tutorial and dropped it into my project (Player3 and Player4), creating the necessary scripts to make it move, as per the sample you kindly provided. The entities follow my character even after I set it’s rigidbody to dynamic, although a bit buggy(i.e. going through slopes when they should go over it, floating at times, etc.).

Seeing that it shouldn’t work at all, would you know why the scripts somehow work for these entities?

Same project:


All rigidbodies are in a simulation physics world. This is updated every frame and then the engine updates the entity’s position and rotation based on the rigid body position and rotation in the physics world

This is way in some cases, you try to set the position of the entity via something like setPosition and it doesn’t move because in the physics update, it’s setting the position based on where the rigid body is in the physics world.

However, if the rigidbody is considered to be sleeping in the physics world (e.g it hasn’t moved for a long time and no other external forces have been applied to it), it won’t set the entity position in the physics update.

In these cases, setPosition will move the entity but not the rigid body so the entity will go through other rigid bodies because the entity’s rigid body hasn’t moved.