Laya商业级3d实战-011角色跑动_流程整合

本节目标:角色跑动,流程整合

laya商业级3d游戏开发

角色类
Game目录下 新建Player.ts

//MonoBehaviour 是框架类,默认拥有gameObject和transform属性
export default class Player extends MonoBehaviour {

localx = 0;
animator: Laya.Animator;
onAwake() {
super.onAwake();
this.animator = this.gameObject.getChildByName(‘CatBase’).getComponent(Laya.Animator);
this.animator.play(‘Cat_RunLong’);
}

onUpdate() {
//左滑手势
if (Input.slideLeft) {
this.localx += 1.25;
}
//右滑手势
if (Input.slideRight) {
this.localx -= 1.25;
}

//插值计算,圆滑移动
this.transform.localPositionX = Mathf.Lerp(this.transform.localPositionX, this.localx, 0.2)

}

}

配合U3D讲解角色类

Game目录下新建 Game.ts
import GameObject from “…/UnityEngine/GameObject”;
import Player from “./Player”;
import { ObstacleSpawn } from “./ObstacleSpawn”;
import { FishSpwan } from “./FishSpwan”;
import BuildSpawn from “./BuildSpawn”;
import SceneManager from “…/UnityEngine/SceneManager”;

export default class Game extends Laya.Script {

static instance: Game;
scene: Laya.Scene3D;
cureentZ = 0;
speed = 0.3;
spwans: BuildSpawn[] = [];
playerPivot: Laya.Sprite3D;
player: Player;
obstacleSpawn: ObstacleSpawn;
onAwake() {
    Game.instance = this;
    this.scene = SceneManager.game;
}

onStart() {

    let bulidjsonStr = '{"findRoot":"Resources/BuildItem","spwanItemDatas":[{"goName":"IndustrialWarehouse01","length":18.0},{"goName":"IndustrialWarehouse03","length":27.0}],"startCreateZ":0.0,"CreateLength":150.0,"recoverOffset":-10.0}';
    let fishjsonStr = '{"findRoot":"Resources/items","spwanItemDatas":[{"goName":"Fish","length":1.0}],"startCreateZ":30.0,"CreateLength":150.0,"recoverOffset":-5.0}';
    let obstaclejsonStr = '{"findRoot":"Resources/items","spwanItemDatas":[{"goName":"ObstacleRoadworksBarrier","length":1.0}],"startCreateZ":30.0,"CreateLength":150.0,"recoverOffset":-5.0}';

    let buildSpwan = new BuildSpawn();
    buildSpwan.spwanConfigObj = JSON.parse(bulidjsonStr);
    buildSpwan.scene = this.scene;
    this.scene.addComponentIntance(buildSpwan);
    this.spwans.push(buildSpwan);



    let fishSpwan = new FishSpwan();
    fishSpwan.spwanConfigObj = JSON.parse(fishjsonStr);
    fishSpwan.scene = this.scene;
    this.scene.addComponentIntance(fishSpwan);
    this.spwans.push(fishSpwan);

    let p_ObstacleSpawn = new ObstacleSpawn();
    p_ObstacleSpawn.spwanConfigObj = JSON.parse(obstaclejsonStr);
    p_ObstacleSpawn.scene = this.scene;
    this.scene.addComponentIntance(p_ObstacleSpawn);
    this.spwans.push(p_ObstacleSpawn);

    this.obstacleSpawn = p_ObstacleSpawn;
    this.playerPivot = this.scene.getChildByName('PlayerPivot') as Laya.Sprite3D;
    let charater = GameObject.Find(this.scene, 'PlayerPivot/charater')
    this.player = charater.addComponent(Player);
}

onUpdate() {

    this.cureentZ += this.speed;
    for (let index = 0; index < this.spwans.length; index++) {
        this.spwans[index].currentZ = this.cureentZ;
    }

}

onLateUpdate() {
    let pos = this.playerPivot.transform.position;
    pos.z = this.cureentZ;
    this.playerPivot.transform.position = pos;
}

}

在scripts下增加

GameSample.ts

import SceneManager from “./UnityEngine/SceneManager”;
import Game from “./Game/Game”;

export class GameSample {
public static StartGame() {
SceneManager.LoadSceneByName(‘Game’, this, this.OnGameSceneLoadOk);
}

static OnGameSceneLoadOk(p_Scene3D: Laya.Scene3D) {
    Laya.stage.addChildAt(p_Scene3D, 0);
    SceneManager.game = p_Scene3D;
    p_Scene3D.addComponent(Game);
}

}

Main.ts
onConfigLoaded(): void {
let node = new Laya.Node();
Laya.stage.addChild(node);
node.addComponent(UnityEnagine);
GameSample.StartGame();
}

F8 f5
在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/koljy111/article/details/108020216