[SOLVED] Out and Back

In this script I need help because I want a certain entity to go out a certain distance then it comes back to its original spot. Somebody please help I have tried everything so far I could think of.

Hi @Mason_Rocchio,

Your request is a bit generic, but check this in case it helps you. It can animate an entity moving towards a point:

https://developer.playcanvas.com/en/tutorials/point-and-click-movement/

I suggest to use a tween for this.

https://developer.playcanvas.com/en/tutorials/tweening/

if(this.app.mouse.ispressed(pc.MOUSEBUTTON_LEFT)){
this.entity
    .tween(this.entity.getLocalPosition())
    .to(new pc.Vec3(4, 0, 0), 1.0, pc.SineOut)
    .loop(true)
    .yoyo(true)
    .start();
}

this is what I typed in for tween and it did not work out.
Whenever I pressed the left mouse button it did not move.
Do I need a kinematic rigidbody so it moves whenever the force is applied?

I forgot to add the script to the entity but now it says
this.entity.tween is not a function

You need to download the tween script and add it to your project.

Ok thanks for the tip

I uploaded it but the tween.js script will not show up when I try to import it.

Screenshot 2022-03-29 5.49.09 PM

It only need to be in your project. Is it in your project?

You mean did I upload it into the project?

Yes, that should be enough.

Yeah I downloaded the file and put it in the project and it would not show up.

You don’t need to add it to an entity. You can try to launch your project again.

I am trying to make a entity go away from the player then come back to the player

If it is an entity that is moving like an enemy AI, then I’m not sure if a tween is the best solution. It depends on how you move the entity, but maybe you can try to use the lookAt function in combination with checking the distance. Based on that you can move the entity forward or backwards.

1 Like

I am confused here, Cant I do something like this?

if(this.app.mouse.ispressed(pc.MOUSEBUTTON_LEFT)){
this.entity.translatelocal(0,0,2);
else{
this.entity.setlocalposition(?,?,?);
     }
};

I’m confused too. You want to use the mouse button to move the entity away from the player and after that move the entity back to the player? What kind of entity it is?

it is a hand that goes away from the player then immediately comes back.
BUT! if it is touching a given entity it stays unless the mouse button is pressed again in this case the left mouse button.

Alright, in that case I’d use a tween like you’re trying to do before. You can check the topic below where something similar is being done.