cocos-docs icon indicating copy to clipboard operation
cocos-docs copied to clipboard

快速上手:制作第一个 3D 游戏

Open baba990aini opened this issue 1 year ago • 0 comments

URL : https://github.com/cocos-creator/creator-docs/blob/master/zh/getting-started/first-game/index.md

I followed your tutorial to write the code, but after adding the animation, my capsule became jumping in place. Here is my code, please help me see where the problem lies

` import { _decorator, Component, Vec3, input, Input, EventMouse, Animation } from 'cc'; const { ccclass, property } = _decorator;

@ccclass("PlayerController") export class PlayerController extends Component { @property({type: Animation}) public BodyAnim: Animation | null = null; private _startJump: boolean = false; private _jumpStep: number = 0; private _curJumpTime: number = 0; private _jumpTime: number = 0.1; private _curJumpSpeed: number = 0; private _curPos: Vec3 = new Vec3(); private _deltaPos: Vec3 = new Vec3(0, 0, 0); private _targetPos: Vec3 = new Vec3(); start () { input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this); } onMouseUp(event: EventMouse) { if (event.getButton() === 0) { this.jumpByStep(1); } else if (event.getButton() === 2) { this.jumpByStep(2); }

}
jumpByStep(step: number) {
    if (this._startJump) {
        return;
    }
    if (this.BodyAnim) {
        if (step === 1) {
            this.BodyAnim.play('oneStep');
        } else if (step === 2) {
            this.BodyAnim.play('twoStep');
        }
    }
    this._startJump = true;
    this._jumpStep = step;
    this._curJumpTime = 0;
    this._curJumpSpeed = this._jumpStep / this._jumpTime;
    this.node.getPosition(this._curPos);
    Vec3.add(this._targetPos, this._curPos, new Vec3(this._jumpStep, 0, 0));
}
onOnceJumpEnd() {
    this._startJump = false;
}
update (deltaTime: number) {
    if (this._startJump) {
        this._curJumpTime += deltaTime;
        if (this._curJumpTime > this._jumpTime) {
            this.node.setPosition(this._targetPos);
            this._startJump = false;
            this.onOnceJumpEnd();
        } else {
            this.node.getPosition(this._curPos);
            this._deltaPos.x = this._curJumpSpeed * deltaTime;
            Vec3.add(this._curPos, this._curPos, this._deltaPos);
            this.node.setPosition(this._curPos);
        }
    }
}

} `

baba990aini avatar Mar 26 '24 07:03 baba990aini