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);
	}
}())
相关推荐
C_心欲无痕14 小时前
js - generator 和 async 函数讲解
开发语言·javascript·ecmascript
林恒smileZAZ14 小时前
前端技巧:检测到省略号文本自动显示 Tooltip
开发语言·前端·javascript
Zzz不能停14 小时前
阻止冒泡和阻止元素默认行为的区别
开发语言·前端·javascript
m0_5287238114 小时前
如何避免多次调用同一接口
前端·javascript·vue.js·性能优化
小高00714 小时前
Elips:领域模型与 DSL 设计实践:从配置到站点的优雅映射
前端·javascript·后端
嫂子的姐夫14 小时前
012-AES加解密:某勾网(参数data和响应密文)
javascript·爬虫·python·逆向·加密算法
德育处主任14 小时前
『NAS』部署轻量级开源图片水印工具-ImageWatermarkTool
前端·javascript·docker
程序员Agions14 小时前
TypeScript 类型体操:从入门到"真香"の完全指南
前端·javascript
pas13614 小时前
28-mini-vue customRender
前端·javascript·vue.js
秋天的一阵风15 小时前
🎥解决前端 “复现难”:rrweb 录制回放从入门到精通
前端·javascript·开源