playcanvas-tween
playcanvas-tween copied to clipboard
Tweening position doesn't work.
@yaustar The same code used to tween local position doesn't work for tweening position. It makes the UI element disappear. Use case: tween from top right corner to top left corner. Related forum post: https://forum.playcanvas.com/t/how-can-i-get-the-top-left-position-if-i-anchored-on-the-top-right/23007
How are you calling the code? The reason it doesn't work IIRC is because the object returned by getPosition is a temp variable object so changing the values in the object doesn't do anything. Whereas getLocalPosition returns the internal vec3 that the entity transform uses.
To get around this, try using the custom update callback:
var entity = this.entity;
var pos = new pc.Vec3(10, 10, 10);
var tween = this.app.tween(pos).to(new pc.Vec3(1,1,1), 1, pc.Linear);
tween.on('update', function (dt) {
entity.setPosition(pos);
});
(PS Please don't @ directly to user unless it is very specific to that user. There are different team members and contributors that own different parts of the platform and codebase. I'm not in all of them)
Ok sorry yaustar. Your solution worked. I think getPosition and getLocalPosition should work in the same way, should't it? This is confusing.
Ok sorry yaustar. Your solution worked. I think getPosition and getLocalPosition should work in the same way, should't it? This is confusing.
It does require some knowledge about what is done under the hood to understand why the two are different.
An entity stores a vec3 for it's local position and that is used to calculate where it's world position is. This is also return by getLocalPosition so directly editing the object that is returned from getLocalPosition will change it's position.
getPosition returns a temporary calculated position vec3. Editing this directly doesn't do anything.
And the root of all this is because JS can't pass or return by value with objects unlike C# and C++. The reason for all these temporary variables is to reduce garbage collection.
Ideally the two should behave the same way and that getLocalPosition should also return a temporary vec3 instead of the one that it is actually using in the engine for the transform. That way, neither getLocalPosition or getPosition would work in the tween and both would have to use the update method instead.