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);
	}
}())
相关推荐
爱上妖精的尾巴2 分钟前
3-16单元格区域尺寸调整(发货单记录保存-方法2)学习笔记
javascript·笔记·学习·wps·js宏·jsa
Hilaku18 分钟前
低代码是“未来”还是“骗局”?作为前端我被内耗到了
前端·javascript·低代码
我想说一句26 分钟前
今天我又被JavaScript的this绑架了!🚨
前端·javascript·面试
AR738 分钟前
基于 VuePress 2.x 与 ElementPlus 的组件库文档搭建实践
前端·javascript
前端康师傅39 分钟前
JavaScript 数据类型详解:从基础到深入
前端·javascript
AR741 分钟前
前端业务组件封装与管理的解决方案
前端·javascript
Hilaku1 小时前
每天都在用的 debounce 和 throttle,其实 80% 的用法都错了
前端·javascript·面试
架构个驾驾1 小时前
前端微服务框架乾坤(Qiankun)实战指南
前端·javascript·vue.js
ch_s_t1 小时前
Babylon.js引擎(二)
开发语言·javascript·ecmascript
layman05281 小时前
前端实战:用 HTML+JS 打造可拖动图像对比滑块,提升视觉交互体
前端·javascript·html