From property in tween library

hello,i am facing a problem with tween library.
if i am running the following code ,i can see my entity moving back and forth…

this.entity.setLocalPosition(-4, 0, 0);
this.tween = this.entity.tween(this.entity.getLocalPosition())
    .to(new pc.Vec3(4, 0, 0), this.duration, pc[this.easing])        .delay(this.delay)
    .loop(this.loop)
    .yoyo(this.yoyo);


this.tween.start();

but when i am changing
this.tween = this.entity.tween(this.entity.getLocalPosition())
to
this.tween = this.entity.tween(new pc.Vec3(-4,0,0))

i can’t see my entity moving.

why my entity is not moving can anyone explain???

You give the tween a starting position when calling tween and a target position via the to function.

And finally you call the start function to start the tween.

ok but
why this is not working

this.tween = this.entity.tween(new pc.Vec3(-4,0,0))
.to(new pc.Vec3(4, 0, 0), this.duration, pc[this.easing]) .delay(this.delay)
.loop(this.loop)
.yoyo(this.yoyo);

when i am directly mentioning the starting position as new pc.Vec3(-4,0,0)

Have you called the start function on the tween?

yes

https://playcanvas.com/editor/code/452634?tabs=6394844

here is the code link if u want.

at line number 36
if u change
this.tween = this.entity.tween(this.entity.getLocalPosition())
to
this.tween = this.entity.tween(new pc.Vec3(-4,0,0))
entity is not moving… :roll_eyes: :roll_eyes: :roll_eyes:

The reason it “does not work” is because you do not see the result of the tween. The reason it did work in the first case, is because you tweened the position property of the entity directly. In the second case you simply increase the X value of new vector, which is not related to the entity. You want to tween the entity property, or assign that vector to the entity property on tween update.

var from = new pc.Vec3(-4, 0, 0);
var to = new pc.Vec3(4, 0, 0);
var entity = this.entity;
entity.tween(from)
    .to(to, 1.0, pc.SineOut)
    .on('update', function(dt) {            
        entity.setPosition(from);
    })
    .start();

That’s my bad on misunderstanding the tween library. this.entity.getLocalPosition() returns a reference to the local position of the entity so when it gets updated, it moves the entity too.

Using a new pc.Vec3 means that it changes the property of that object and not the local position of the entity.

1 Like

ohhhhh,
ok that make sense now.
thanx to both of u guys for the help :slightly_smiling_face:

1 Like