0

Creator version: 3.7.2

Target platform: Chrome

Problem: Why doesn't the animation move when using a script to edit the Animation animation, I've confirmed that the track is already mounted on AnimationClip! I've read and re-read the official documentation for this programmatically edited animation clip many times. Comparing my code, I really can't find what the problem is.

Hierarchy: roleSK(prefab)/choose(Node)/arrow(Node)

Here is the script code

`

private _setAnimationPosition() {
    const ani = this._arrow.getComponent(Animation);
if (!ani) {
    console.error('Animation component not found on arrow node!');
    return;
}

const clip = ani.defaultClip;
if (!clip) {
    console.error('Default AnimationClip not found on arrow node!');
    return;
}

console.log('Animation component:', ani);
console.log('Default animation clip:', clip);

clip.clearTracks();
clip.duration = 1.0; // 整个动画剪辑的周期
const track = new animation.VectorTrack(); // 创建一个向量轨道
track.componentsCount = 3; // 使用向量轨道的前三条通道
const path = new animation.TrackPath()
    .toHierarchy('arrow') // 设置层级路径
    .toProperty('position'); // 指定要控制的属性
track.path = path

console.log('Animation position track set:', track);

const arrowHeight = this._arrow.position.y;
const vec3KeyFrames = [
    [0, new Vec3(0, arrowHeight + 100, 0)],
    [0.5, new Vec3(0, arrowHeight, 0)],
    [1, new Vec3(0, arrowHeight + 100, 0)]
] as [number, Vec3][];

const [x, y, z] = track.channels();
x.curve.assignSorted(vec3KeyFrames.map(([time, vec3]) => [time, { value: vec3.x }]));
y.curve.assignSorted(vec3KeyFrames.map(([time, vec3]) => [time, { value: vec3.y }]));
z.curve.assignSorted(vec3KeyFrames.map(([time, vec3]) => [time, { value: vec3.z }]));

console.log('Keyframes:', vec3KeyFrames);

clip.addTrack(track); // 将轨道添加到动画剪辑
clip.wrapMode = AnimationClip.WrapMode.Loop; // 设置动画循环模式

ani.defaultClip = clip; // 重新分配修改后的 AnimationClip
ani.play(); // 播放动画

}

`

Suspect 1: I didn't write the path correctly when I set the path, but the paths 'roleSK/choose/arrow', 'choose/arrow', and 'arrow' have all been tried, and arrow still doesn't work. tried, and arrow still doesn't move.

0

Browse other questions tagged or ask your own question.