[SOLVED] Button to make something tween and then stop

Hiya,

I have a script which I’ve taken from some where else. I would like an object to move from one position to another on a button click. This script does do that… however it then snaps back to the original position on completion. Is there an easy way to make it hold once it goes to it’s new position.

thanks!

var ButtonTween = pc.createScript('buttonTween');

ButtonTween.attributes.add('button', {
    type: 'entity',
    title: 'Button',
    description: 'The Button being clicked on.'
});

ButtonTween.attributes.add('tweenObject', {
    type: 'entity',
    title: 'Object',
    description: 'The Object to tween'
});

ButtonTween.prototype.initialize = function() {
    
    this.origPos = this.tweenObject.getLocalPosition().clone();   
    this.origRot = this.tweenObject.getLocalEulerAngles().clone();
    this.targetPos = new pc.Vec3(4.0, 3.5, -4.0);
    this.targetRot = new pc.Vec3(-30, 135, 0);
    
    this.targetPosTween = this.tweenObject.tween(this.tweenObject.getLocalPosition())

        .to(this.targetPos, 2.0, pc.Linear);
  
    this.targetRotTween = this.tweenObject.tween(this.tweenObject.getLocalEulerAngles())   

        .rotate(this.targetRot, 2.0, pc.Linear);
      
    this.button.button.on('click', this.onButtonClick, this);
  
};

ButtonTween.prototype.onButtonClick = function() {
    
    if(!this.completed) {

        this.targetPosTween.start();
        this.targetRotTween.start();
        this.completed = true; 
    }
};

Hi @Mattturner2021,

Your code seems correct, I don’t see any issue with that. The object will tween from start to end pos, and that’s what this code will only do.

Maybe you have some other script that repositions the object to the original position, hence the snapping?

Try sharing a sample project so we can take a look.

3 Likes

yep it was a script on the camera :slight_smile:

1 Like