Laya1.8.4在web环境下上下前后左右移动相机

javascript 复制代码
(function(){
      var Vector3 = laya.d3.math.Vector3;
    var KeyBoardManager = laya.events.KeyBoardManager;
   
     var CameraMoveUtils = function(){

    }

    var _proto = CameraMoveUtils.prototype

    _proto.InitCamera = function(camera){
        this.camera = camera;
        this.cameraRotation = new Vector3();
        this._tempVector3 = new Vector3();
        this.speed = 0.01;
        Laya.timer.frameLoop(2,this,this.onUpdate);
    }

    _proto.onUpdate = function(){
        var elapsedTime = Laya.timer.delta;
        KeyBoardManager.hasKeyDown(87) && this.moveForward(-this.speed * elapsedTime);//W
        KeyBoardManager.hasKeyDown(83) && this.moveForward(this.speed * elapsedTime);//S
        KeyBoardManager.hasKeyDown(65) && this.moveRight(-this.speed * elapsedTime);//A
        KeyBoardManager.hasKeyDown(68) && this.moveRight(this.speed * elapsedTime);//D
        KeyBoardManager.hasKeyDown(81) && this.moveVertical(this.speed * elapsedTime);//Q
        KeyBoardManager.hasKeyDown(69) && this.moveVertical(-this.speed * elapsedTime);//E
    }
	/**
	 * 向前移动。
	 * @param distance 移动距离。
	 */
	_proto.moveForward = function(distance){
		this._tempVector3.x = this._tempVector3.y = 0;
		this._tempVector3.z = distance;
		this.camera.transform.translate(this._tempVector3);
	}

	/**
	 * 向右移动。
	 * @param distance 移动距离。
	 */
	_proto.moveRight = function(distance) {
		this._tempVector3.y = this._tempVector3.z = 0;
		this._tempVector3.x = distance;
		this.camera.transform.translate(this._tempVector3);
	}

	/**
	 * 向上移动。
	 * @param distance 移动距离。
	 */
	_proto.moveVertical = function(distance) {
		this._tempVector3.x = this._tempVector3.z = 0;
		this._tempVector3.y = distance;
		this.camera.transform.translate(this._tempVector3, false);
	}
}())
相关推荐
GISer_Jing9 小时前
DeepSeek 阐述 2025年前端发展趋势
前端·javascript·react.js·前端框架
prince_zxill9 小时前
RESTful 架构原则及其在 API 设计中的应用
前端·javascript·架构·前端框架·restful
geovindu11 小时前
D3.js Org Chart
开发语言·javascript·ecmascript
疋瓞11 小时前
excel实用问题:提取文字当中的数字进行运算
java·javascript·excel
CodeClimb12 小时前
【华为OD-E卷 - 任务最优调度 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
Chaoran12 小时前
vue3 封装右键菜单组件
前端·javascript
yqcoder13 小时前
Node 服务器数据响应类型处理
运维·服务器·前端·javascript·node.js
brahmsjiang13 小时前
React中的JavaScript语法
前端·javascript·react.js
prince_zxill13 小时前
深入探索Vue 3组合式API
前端·javascript·网络·vue.js·前端框架
16年上任的CTO14 小时前
vue生命周期及其作用
前端·javascript·vue.js·vue