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);
	}
}())
相关推荐
Red Car41 分钟前
javascript 性能优化实例一则
开发语言·javascript·ecmascript
艾小码1 小时前
从Hello World到变量数据类型:JavaScript新手避坑指南
前端·javascript
千叶寻-2 小时前
正则表达式
前端·javascript·后端·架构·正则表达式·node.js
記億揺晃着的那天9 小时前
Vue + Element UI 表格自适应高度如何做?
javascript·vue.js·ui
GISer_Jing10 小时前
ByteDance——jy真题
前端·javascript·面试
真的想不出名儿10 小时前
Vue 中 props 传递数据的坑
前端·javascript·vue.js
阳光阴郁大boy10 小时前
星座运势网站技术解析:从零打造现代化Web应用
前端·javascript
sorryhc10 小时前
如何设计一个架构良好的前端请求库?
前端·javascript·架构
Queen_sy10 小时前
vue3 el-date-picker 日期选择器校验规则-选择日期范围不能超过七天
javascript·vue.js·elementui
lvchaoq11 小时前
react 修复403页面无法在首页跳转问题
前端·javascript·react.js