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);
	}
}())
相关推荐
林太白23 分钟前
CSS长度单位px、rem、em、vh、vw
前端·javascript·css
一只小风华~1 小时前
BOM Cookie操作详解
开发语言·前端·javascript
小高0071 小时前
⚡前端底层四连击:Event Loop → 渲染帧 → GC → AST,一篇打通任督二脉
前端·javascript·面试
今禾1 小时前
# 🚀 深入浅出 TypeScript(下):掌握泛型、接口及 React 中的高级应用
前端·javascript·typescript
GISer_Jing2 小时前
浏览器缓存机制全解析:强缓存与协商缓存
前端·javascript·缓存
WebInfra2 小时前
深度剖析 tree shaking:主流打包工具的实现对比
前端·javascript·架构
weixin_471525782 小时前
【学习嵌入式day-17-数据结构-单向链表/双向链表】
前端·javascript·html
jingling5552 小时前
Git 常用命令指南:从入门到高效开发
前端·javascript·git·前端框架
萌萌哒草头将军2 小时前
VoidZero 发布消息称 Vite 纪录片即将首映!🎉🎉🎉
javascript·vue.js·vite
浮桥3 小时前
vue3渲染html数据并实现文本修改
javascript·vue.js·html