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);
	}
}())
相关推荐
张元清10 小时前
React Observer Hooks:7 种监听 DOM 而不写样板代码的方式
前端·javascript·面试
竹林81811 小时前
Next.js + wagmi v2 踩坑实录:开发 NFT 交易市场时,我如何处理离线签名和链下元数据
javascript·next.js
前端Hardy11 小时前
谁还没⽤过shadcn/ui?114k+星标,不装NPM包,前端组件自由终于实现了
前端·javascript·vue.js
猪猪聪明_V11 小时前
前端码农的本地项目启动器
前端·javascript
暗不需求12 小时前
前端性能优化 防抖与节流完全指南:从原理到最佳实践
前端·javascript·面试
@大迁世界12 小时前
45.什么是内联条件表达式(inline conditional expressions)?在事件处理里怎么用?
开发语言·前端·javascript·react.js·ecmascript
我胖虎不答应!!12 小时前
Three.js开发思想笔记
javascript·笔记·three.js
一颗趴菜12 小时前
微信小程序如何去下载PDF呢
前端·javascript
zithern_juejin13 小时前
JS深拷贝与浅拷贝
javascript
前端毕业班13 小时前
前端"枚举"管理指南
前端·javascript