[SOLVED] this.entity.translateLocal(0,0,10) in update not working

I am trying to make an entity go up and down continuously it’s this.entity.translateLocal() I want it to go up ten and town ten. Yes, I did put this code in update. Here is my code

This.entity.translateLocal(0,0,-10) // to go up
This.entity.translateLocal(0,0,10) // to go down

Hi @Codeknight999! I think line 2 is overwriting line 1 if you do it in the same update loop. Maybe you can use a boolean or something like that to keep track of the current position.

how would I do that? I have never used a boolean in javascript.

Maybe you can also do something like below?

if (this.entity.getLocalPosition().z === 10) {
    this.entity.setLocalPosition(0, 0, -10);
}
else {
    this.entity.setLocalPosition(0, 0, 10);
}

Can setLocalPosition be replaced with translateLocal ?

Yes, in that case it’s better to use a boolean.

// initialize 
this.up = false;
// update 
if (this.up) {
    this.up = false;
    this.entity.translateLocal(0, 0, -10);
}
else {
    this.up = true;
    this.entity.translateLocal(0, 0, 10);
}


The up and down movement seems to work, but it is so fast it jitters, is there a way to do a timeout between going up and down?

Yes, you can create a timer.

Maybe a tween is a better option?

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

I have the tween.js file, how do I use the tween api, would I put it in another script?

Yes, that’s correct. You can find examples on the page I shared above.

Thanks, I am currently trying out the code.

1 Like

When I run the code it says entity is not defined, I am using this code:
entity.tween(entity.getLocalPosition()).to({x: 10, y: 0, z: 0}, 1, pc.SineOut);
I also tried
this this.entity.tween(entity.getLocalPosition()).to({x: 10, y: 0, z: 0}, 1, pc.SineOut);

There is a second entity here that probably need to be this.entity.

So what is the code? I dont understand what you said

What I said is not that difficult. Please check the line of code.

I used the this.entity and it still says entity is not defined

On both places?

I am just the first, but it usually only shows one error per script at a time

What about the second entity in that line of code?

I commented the first line out and the 2nd entity says the same thing.