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);
	}
}())
相关推荐
circuitsosk2 小时前
任务规划器的三种范式对比:ReAct、Plan-and-Execute 与 Tree-of-Thought 在真实业务中的取舍
前端·javascript·python·react.js·llm·ai agent
小磊哥er3 小时前
深入解构Claude Code - 第 12 篇 · 整体串起来
javascript·ai编程
小磊哥er3 小时前
深入解构Claude Code - 第 11 篇 · 工程上的讲究
javascript·ai编程
独立开发之道6 小时前
【three.js教程】Three.js 运行时更新:矩阵、几何体、材质怎么改才不卡
javascript·矩阵·材质
默_笙7 小时前
🌃 HTTP 不认识你:JWT 登录鉴权的完整"酒店入住"指南
前端·javascript
奋斗吧程序媛7 小时前
CSV文件导入功能
开发语言·javascript·ecmascript
梨想橙汁8 小时前
JS 核心语法:运算符流程控制、函数、数组与对象实战详解
前端·javascript
七牛开发者8 小时前
拆解 dsh:Turn 与 Step 如何组织 Agent 主循环
前端·javascript·人工智能
空想兔8 小时前
前端网络请求的前世今生:从 Ajax、XHR 到 Fetch 和 Axios
javascript
梨想橙汁8 小时前
JavaScript 零基础入门:引入方式、变量与数据类型,吃透原始与引用类型
前端·javascript