Laya3D基礎和物體移動到某一點

一:Laya運用:

1:普通加載資源:

let resource = [
            "文件地址1",
            "文件地址2"
        ];
Laya.loader.create(resource, Laya.Handler.create(this, this.onPreLoadFinish));
onPreLoadFinish(){
。。。=Laya.loader.getRes("文件地址1");
。。。=Laya.loader.getRes("文件地址2");
}

然後場景添加在舞臺上,3D物體添加在場景上;

2:動畫的播放:

在unity中添加好所有的動畫狀態;
unity中添加動畫狀態
在Laya中重複添加動畫狀態會報錯;
加載對應的資源之後:

。。。 = this.物體.getComponent(Laya.Animator);
。。。.play("idle");

就能實現播放動畫。

3:實現unity中的moveto方法的效果:

export default class Sprite3DMoveContorller {
    static frompostion: Laya.Vector3;
    static topostion: Laya.Vector3;
    static moveDistance: Laya.Vector3;
    static moveObjcet: Laya.Sprite3D;
    static moveTime: number = 0;
    static moveValue: number = 0.01;
    static callback: Function;
    static obj: any;

    /**
     * Sprite3D移動方法
     * @param gameObject 需要移動的3D對象
     * @param toPos 移動目標點
     * @param playTime 移動總耗時
     * @param easeFun 緩動函數 (Laya.Ease) 默認爲線性移動
     * @param obj 回調域
     * @param callback 回調函數 
     */
    public static onPositionMoveTo(gameObject: Laya.Sprite3D, toPos: Laya.Vector3, playValue: number, easeFun: Function, obj: any = null, callback: Function = null) {
        this.moveDistance = new Laya.Vector3(toPos.x - gameObject.transform.position.x, toPos.y - gameObject.transform.position.y, toPos.z - gameObject.transform.position.z);
        this.frompostion = gameObject.transform.position;
        this.topostion = toPos;
        this.moveObjcet = gameObject;
        this.moveValue = playValue;
        this.callback = callback;
        this.obj = obj;
        this.startMove();
    }

    private static startMove() {
        Laya.timer.frameLoop(1, this, this.moveUpdate);
    }

    private static endMove(handler: Laya.Handler) {
        Laya.timer.clear(this, this.moveUpdate);
        //this.moveObjcet.transform.position = this.topostion;
        console.log(handler);
        if (handler != null) {
            handler.run();
        }
    }

    private static moveUpdate() {
        let a = Laya.Vector3.distance(this.moveObjcet.transform.position, this.topostion);
        if (a < 0.1) {
            this.endMove(Laya.Handler.create(this.obj, this.callback));
        } else {
            this.moveObjcet.transform.position = new Laya.Vector3(
                this.frompostion.x + (this.moveDistance.x * this.moveValue),
                this.frompostion.y + (this.moveDistance.y * this.moveValue),
                this.frompostion.z + (this.moveDistance.z * this.moveValue)
            );
        }
    }
}

實際就是改變物體的座標;

總結

加油!努力!

就回來吧~~~回來吧~~~~有人~在等你啊!!!!
有人~在~等你~說完~那句~說一半的話~~~~
就別走了~~~留下吧~~~~外面~它太複雜~~~~
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章