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);
	}
}())
相关推荐
进击的尘埃1 小时前
Vue3 响应式原理:从 Proxy 到依赖收集,手撸一个迷你 reactivity
javascript
willow2 小时前
JavaScript数据类型整理1
javascript
LeeYaMaster2 小时前
20个例子掌握RxJS——第十一章实现 WebSocket 消息节流
javascript·angular.js
UIUV2 小时前
RAG技术学习笔记(含实操解析)
javascript·langchain·llm
颜酱4 小时前
理解二叉树最近公共祖先(LCA):从基础到变种解析
javascript·后端·算法
FansUnion4 小时前
我如何用 Next.js + Supabase + Cloudflare R2 搭建壁纸销售平台——月成本接近 $0
javascript
左夕6 小时前
分不清apply,bind,call?看这篇文章就够了
前端·javascript
滕青山6 小时前
文本行过滤/筛选 在线工具核心JS实现
前端·javascript·vue.js
时光不负努力6 小时前
编程常用模式集合
前端·javascript·typescript
大雨还洅下6 小时前
前端JS: 跨域解决
javascript