import { Laya } from "Laya" ; import { Script3D } from "laya/d3/component/Script3D" ; import { BaseCamera } from "laya/d3/core/BaseCamera" ; import { Camera } from "laya/d3/core/Camera" ; import { Scene3D } from "laya/d3/core/scene/Scene3D" ; import { Quaternion } from "laya/d3/math/Quaternion" ; import { Vector3 } from "laya/d3/math/Vector3" ; import { Event } from "laya/events/Event" ; import { KeyBoardManager } from "laya/events/KeyBoardManager" ; author export class CameraMoveScript extends Script3D { private protected _tempVector3 : Vector3 = new Vector3 ( ) ; protected lastMouseX : number ; protected lastMouseY : number ; protected yawPitchRoll : Vector3 = new Vector3 ( ) ; protected resultRotation : Quaternion = new Quaternion ( ) ; protected tempRotationZ : Quaternion = new Quaternion ( ) ; protected tempRotationX : Quaternion = new Quaternion ( ) ; protected tempRotationY : Quaternion = new Quaternion ( ) ; protected isMouseDown : boolean ; protected rotaionSpeed : number = 0.00006 ; protected camera : BaseCamera ; protected scene : Scene3D ; speed : number = 0.01 ; constructor ( ) { super ( ) ; } private protected _updateRotation ( ) { if ( Math . abs ( this . yawPitchRoll . y ) < 1.50 ) { Quaternion . createFromYawPitchRoll ( this . yawPitchRoll . x , this . yawPitchRoll . y , this . yawPitchRoll . z , this . tempRotationZ ) ; this . tempRotationZ . cloneTo ( this . camera . transform . localRotation ) ; this . camera . transform . localRotation = this . camera . transform . localRotation ; } } inheritDoc onAwake ( ) { Laya . stage . on ( Event . RIGHT_MOUSE_DOWN , this , this . mouseDown ) ; Laya . stage . on ( Event . RIGHT_MOUSE_UP , this , this . mouseUp ) ; this . camera = ( < Camera > this . owner ) ; } inheritDoc onUpdate ( ) { var elapsedTime = Laya . timer . delta ; if ( ! isNaN ( this . lastMouseX ) && ! isNaN ( this . lastMouseY ) && this . isMouseDown ) { var scene = this . owner . scene ; KeyBoardManager . hasKeyDown ( 87 ) && this . moveForward ( - this . speed * elapsedTime ) ; KeyBoardManager . hasKeyDown ( 83 ) && this . moveForward ( this . speed * elapsedTime ) ; KeyBoardManager . hasKeyDown ( 65 ) && this . moveRight ( - this . speed * elapsedTime ) ; KeyBoardManager . hasKeyDown ( 68 ) && this . moveRight ( this . speed * elapsedTime ) ; KeyBoardManager . hasKeyDown ( 81 ) && this . moveVertical ( this . speed * elapsedTime ) ; KeyBoardManager . hasKeyDown ( 69 ) && this . moveVertical ( - this . speed * elapsedTime ) ; var offsetX = Laya . stage . mouseX - this . lastMouseX ; var offsetY = Laya . stage . mouseY - this . lastMouseY ; var yprElem = this . yawPitchRoll ; yprElem . x -= offsetX * this . rotaionSpeed * elapsedTime ; yprElem . y -= offsetY * this . rotaionSpeed * elapsedTime ; this . _updateRotation ( ) ; } this . lastMouseX = Laya . stage . mouseX ; this . lastMouseY = Laya . stage . mouseY ; } inheritDoc onDestroy ( ) { Laya . stage . off ( Event . RIGHT_MOUSE_DOWN , this , this . mouseDown ) ; Laya . stage . off ( Event . RIGHT_MOUSE_UP , this , this . mouseUp ) ; } protected mouseDown ( e ) { this . camera . transform . localRotation . getYawPitchRoll ( this . yawPitchRoll ) ; this . lastMouseX = Laya . stage . mouseX ; this . lastMouseY = Laya . stage . mouseY ; this . isMouseDown = true ; } protected mouseUp ( e ) { this . isMouseDown = false ; } protected mouseOut ( e ) { this . isMouseDown = false ; } param distance moveForward ( distance ) { this . _tempVector3 . x = this . _tempVector3 . y = 0 ; this . _tempVector3 . z = distance ; this . camera . transform . translate ( this . _tempVector3 ) ; } param distance moveRight ( distance ) { this . _tempVector3 . y = this . _tempVector3 . z = 0 ; this . _tempVector3 . x = distance ; this . camera . transform . translate ( this . _tempVector3 ) ; } param distance moveVertical ( distance ) { this . _tempVector3 . x = this . _tempVector3 . z = 0 ; this . _tempVector3 . y = distance ; this . camera . transform . translate ( this . _tempVector3 , false ) ; } }