How can i change two entities swap with tween functionality

var currentposition = new pc.Entity();
 var secondposition = new pc.Entity();




    if(firstattempt)
    {
        firstattempt=false;
        currentposition=this.handle;
        console.log("current position is ",currentposition);
        this.btncolor=true;
    }
    else
    {

        secondposition=this.handle;
        this.swapfunction(currentposition,secondposition);

    }



function.prototype.swapfunction = function(firstpos,secondpos)
{
    console.log("local position of firstpos ",firstpos.getLocalPosition());
    console.log("local position of secondpos ",secondpos.getLocalPosition());


    firstpos
        .tween(firstpos.getLocalPosition())
        .to(new pc.Vec3(secondpos.getLocalPosition), 0.5, pc.SineOut)
        .loop(false)
        .yoyo(true)
        .start();


    secondpos
        .tween(firstpos.getLocalPosition())
        .to(new pc.Vec3(firstpos.getLocalPosition),0.5, pc.SineOut)
        .loop(false)
        .yoyo(true)
        .start(); 

};

Clone their position and then apply the tween.
var currentObj1 = new pc.Entity();
var currentposition = currentObj1 .getPosition().clone;
Then use the cloned position.(currentposition)

Problems with you script:
You are taking local position that will be reference to the parent. The way you are doing is you are getting a reference to that value. The value will change when you apply tween.
firstpos.getLocalPosition();

Read it:
https://developer.playcanvas.com/en/api/pc.Entity.html

getLocalPosition is a method not a property.

So, in your script “firstpos.getLocalPosition” should be "firstpos.getLocalPosition(). You will still need to pass the cloned value as it will change over time.

1 Like