物理在动画间切换的问题 with spine-pixi
hh_x Your question was originally posted as a reply to the question thread about physics issues in Unity, but I split the thread since the runtime you are using is spine-pixi.
Note that updateWorldTransform(spine.Physics.reset)
should be called after the next animation has been applied, because when this is called, the physics are reset for the current pose. In other words, if it is called before the next animation is set, it may not work because the physics will just be reset for the previous animation pose. I am not very familiar with how to specify the order of execution in spine-pixi, so if you are not sure about that, you might want to wait for an answer from @Davide .
Hello
Here is the function that is invoked by the ticker attached to the Spine object.
As you can see, it performs the following operations:
...
this.state.update(time);
this.skeleton.update(time);
const { skeleton } = this;
this.state.apply(skeleton);
this.beforeUpdateWorldTransforms(this);
skeleton.updateWorldTransform(Physics.update);
this.afterUpdateWorldTransforms(this);
...
The skeleton.updateWorldTransform
is always updated with Physics.update
.
You can approach this in two ways:
- Manually write your own update logic.
- Use the
afterUpdateWorldTransforms
hook to invokeupdateWorldTransform
withPhysics.reset
.
The second approach is more straightforward, though slightly tricky. You need to set Physics.reset
for a single frame and then revert it; otherwise, the physics system will stop working. Here's an example of how you could implement this:
spineObject.state.setAnimation(0, "eyeblink", true); // Set a new animation
const prevAfterUWT = spineObject.afterUpdateWorldTransforms; // Store the current version of afterUpdateWorldTransforms
// Set a custom afterUpdateWorldTransforms hook to be invoked just once
spineObject.afterUpdateWorldTransforms = () => {
// Invoke updateWorldTransform with Physics.reset
spineObject.skeleton.updateWorldTransform(spine.Physics.reset);
// Restore the previous afterUpdateWorldTransforms hook
spineObject.afterUpdateWorldTransforms = prevAfterUWT;
};