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

Search for the second entity in this line of code please.

Ohhhh!!! Now I get it! Let me try that.

I am not getting an error, but the entity doesn’t move up and down.
I even tried replacing the coordinates in code.

@Codeknight999 I don’t have a lot of context from the question above but, can you share a little more code where you are using the tween routines? Also, is this code attached to an entity. I have used tween in this example to open and close doors. Maybe this will help.

https://playcanvas.com/editor/scene/1437677

Also, I am moving a gate on a castle door up using this code.

// Open Door action
GateControl.prototype.open = function() {
    var locPos = this.entity.getLocalPosition();
    this.tween = this.entity.tween(locPos).to({x: 0, y: this.endPoint, z: 0}, this.duration, pc.Linear);
    this.tween.start();
};

// Close Door action
GateControl.prototype.close = function() {
    var locPos = this.entity.getLocalPosition();
    this.tween = this.entity.tween(locPos).to({x: 0, y: this.startPoint, z: 0}, this.duration, pc.Linear);
    this.tween.start();
};

// Open Door event handler
GateControl.prototype.handleOpen = function() {
    var locPos = this.entity.getLocalPosition();
    this.tween = this.entity.tween(locPos).to({x: 0, y: this.endPoint, z: 0}, this.duration, pc.Linear);
    this.tween.start();

    // Timer loop    

};

// Close Door event handler
GateControl.prototype.handleClosed = function() {
    var locPos = this.entity.getLocalPosition();
    this.tween = this.entity.tween(locPos).to({x: 0, y: this.startPoint, z: 0}, this.duration, pc.Linear);
    this.tween.start();
};

I checked the manual page and the correct code is on the page. If you read you know what code to use. That’s not always the first line of code you see. Please note that the code should not be used in the update function. In your case you can use it in the initialize function.

1 Like

I looked at the tween library project and I used the tween position script. No errors but my entity is not there when I run the code, but it is there in editor.

Can you show your script?

This is the first script I was using, it did nothing:

//initilize
this.entity.tween(this.entity.getLocalPosition()).to({x: 0, y: 0, z: -10}, 1, pc.SineOut);
this.tween.start();

The second script I found, made my entity not show:

var TweenPosition = pc.createScript('tweenPosition');

TweenPosition.attributes.add('duration', {type: 'number', default: 1.0});
TweenPosition.attributes.add('easing', {type: 'string', default: 'Linear'});
TweenPosition.attributes.add('delay', {type: 'number', default: 0});
TweenPosition.attributes.add('loop', {type: 'boolean', default: true});
TweenPosition.attributes.add('yoyo', {type: 'boolean', default: false});
TweenPosition.attributes.add('repeat', {type: 'number', default: 2});

TweenPosition.prototype.initialize = function() {
    // create our tween
    this.reset();
    
    // handle attribute changes
    this.on('attr:duration', function (value) {
        this.tween.duration = value;
    }, this);
    
    this.on('attr:easing', this.reset, this);
    this.on('attr:delay', this.reset, this);    
    this.on('attr:loop', this.reset, this);    
    this.on('attr:yoyo', this.reset, this);    
    this.on('attr:repeat', this.reset, this);
};

TweenPosition.prototype.reset = function () {                   
    // if we are already tweening then stop first
    if (this.tween) {
        this.tween.stop();
    }
    
    // reset our position
    this.entity.setLocalPosition(-4, 0, 0);
    
    // create a new tween using our script attributes
    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);
    
    // only set repeats if loop is false
    if (! this.loop)
        this.tween.repeat(this.repeat);
    
    // start the tween
    this.tween.start();
};


As far I can see you only need the code below from the manual page. (I only changed the axis).

this.entity
    .tween(this.entity.getLocalPosition())
    .to(new pc.Vec3(0, 0, 10), 1.0, pc.SineOut)
    .loop(true)
    .yoyo(true)
    .start();

You can try it in your initialize function.

I think the Z axis of the entity in the scene should be -10 to achieve the desired result.

The entity is moving but not to the place it should. It should be going up and down but it is going diagonally down. Any ideas on how to get it to go up and down?

Here is a video of what is happening.

If you share a link to your project, I will take a look when I have some time later today.

What is the name of the scene and entity?

There is only one scene, here are the steps to get to the entity:

  1. select Factory (North)
  2. select Building
  3. select Factory working parts
  4. select hammer
    here is the link: PlayCanvas 3D HTML5 Game Engine
1 Like

I don’t know in which direction the entity should move, but the code below solves the problem.

var pos = this.entity.getLocalPosition();
this.entity
    .tween(this.entity.getLocalPosition())
    .to(new pc.Vec3(pos.x, pos.y, pos.z + 10), 1.0, pc.SineOut)
    .loop(true)
    .yoyo(true)
    .start();
};

You can change the axis and value to get the movement you want.

Thanks a lot, Albertos! This seems to work! :smile: Thanks for all your help!

1 Like