Bläddra i källkod

相机朝向目标点

woso_javan 1 månad sedan
förälder
incheckning
bdec2349b6

+ 1 - 0
assets/module_aliens/Script/Components/RadarComponent.ts

@@ -42,6 +42,7 @@ export class RadarComponent extends Component {
             
             // 移动雷达指示器
             this.node.setPosition(localPos);
+            EventDispatcher.instance.emit(GameEvent.EVENT_CAMERA_SCREENSHOT_RADAR_LOCK,this._targetNode); // 发送事件通知相机已准备好进行截图,传递当前的 _targetNode 作为参数,用于在相机组件中获取目标节点的位置和旋转信息。
         }
     }
 

+ 3 - 0
assets/module_aliens/Script/Enum/GameEvent.ts

@@ -24,6 +24,9 @@ export class GameEvent {
     /** 取消侦探*/
     static readonly EVENT_CAMERA_SCREENSHOT_RADAR_CANCEL = 'EVENT_CAMERA_SCREENSHOT_RADAR_CANCEL';
 
+    /** 相机对准目标*/
+    static readonly EVENT_CAMERA_SCREENSHOT_RADAR_LOCK = 'EVENT_CAMERA_SCREENSHOT_RADAR_LOCK';
+
     /** 击中文本提示测试*/
     static readonly EVENT_CAMERA_SHOOT_TEXT = 'EVENT_CAMERA_SHOOT_TEXT';
 

+ 29 - 0
assets/module_aliens/Script/LevelAction.ts

@@ -74,6 +74,7 @@ export class LevelAction extends Component {
         EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_AIM,this.onAimTarget,this);
         EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_RESET_AIM,this.onResetAimTarget,this);
         EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_SHOOT,this.onShoot,this);
+        EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_SCREENSHOT_RADAR_LOCK,this.onCameraToTarget,this);
     }
 
     private unRegisterEvent(){
@@ -87,6 +88,7 @@ export class LevelAction extends Component {
         EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_AIM,this.onAimTarget,this);
         EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_RESET_AIM,this.onResetAimTarget,this);
         EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_SHOOT,this.onShoot,this);
+        EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_SCREENSHOT_RADAR_LOCK,this.onCameraToTarget,this);
     }
 
     private onAimTarget(){
@@ -151,6 +153,33 @@ export class LevelAction extends Component {
             .start();
     }
 
+    //相机转向目标
+    private async onCameraToTarget(targetNode: Node){
+        const camera = this.camera.node;
+        const targetPos = targetNode.worldPosition.clone();
+        
+        // 保存初始旋转状态
+        const initialRotation = this._originalRotation.clone();
+        
+        // 计算目标相对于相机的方向
+        const direction = new Vec3();
+        Vec3.subtract(direction, targetPos, camera.worldPosition);
+        direction.normalize();
+        
+        // 基于初始旋转状态计算新角度
+        const newRotation = new Vec3();
+        newRotation.y = initialRotation.y - direction.x * 10; // 调整系数
+        newRotation.x = initialRotation.x + direction.y * 10;
+        
+        // 应用旋转限制
+        newRotation.x = Math.max(this.minXRotation, Math.min(this.maxXRotation, newRotation.x));
+        newRotation.y = Math.max(this.minYRotation, Math.min(this.maxYRotation, newRotation.y));
+        
+        // 设置相机旋转
+        camera.setRotationFromEuler(newRotation);
+        await this.saveCameraState();
+    }
+
     private adjustRotationLimits() {
         // 计算当前镜头距离比例 (0-1范围)
         const distanceRatio = Math.min(1, Math.max(0, (this.camera.node.position.z - this._cameraOriginalPos.z) / -30));