Unity中通过文本输入与按钮实现2D飞行器旋转(雷达模拟)
Unity飞行器雷达模拟系统优化实现
需求说明
- 支持文本输入目标角度,点击左转/右转按钮控制飞行器(绿色圆形)旋转至目标角度
- 定义360度为正北方向
- 旋转时自动选择最短路径(例如转向270度时优先走左转路径)
- 当前已实现按钮基础控制,但旋转度数为硬编码,需优化逻辑
现有问题代码
左侧按钮脚本(Left.cs)
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class Left : MonoBehaviour, IPointerDownHandler,IPointerUpHandler { bool ispressed = false; public GameObject Aircraft; void Update() { if (ispressed) { Aircraft.transform.Rotate(-90.0f, 0, 0, Space.World); } } public void OnPointerDown(PointerEventData eventData) { ispressed = true; } public void OnPointerUp(PointerEventData eventData) { ispressed = false; } }
右侧按钮脚本(Right.cs)
注:此脚本存在逻辑错误,使用了
Translate(平移)而非Rotate(旋转),需修正
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class Right : MonoBehaviour, IPointerDownHandler,IPointerUpHandler { bool ispressed = false; public GameObject Aircraft; void Update() { if (ispressed) { Aircraft.transform.Translate(0.2f, 0, 0); } } public void OnPointerDown(PointerEventData eventData) { ispressed = true; } public void OnPointerUp(PointerEventData eventData) { ispressed = false; } }
优化实现方案
1. 核心旋转控制脚本
创建AircraftRotationController脚本统一管理角度逻辑,自动处理最短路径旋转:
using UnityEngine; using UnityEngine.UI; public class AircraftRotationController : MonoBehaviour { public GameObject aircraft; public InputField angleInput; public float rotationSpeed = 90f; // 旋转速度,Inspector可调 private float targetAngle = 0f; private float currentAngle = 0f; void Start() { // 初始化角度为当前飞行器朝向(适配原有X轴旋转逻辑) currentAngle = Mathf.Repeat(aircraft.transform.eulerAngles.x, 360f); targetAngle = currentAngle; } void Update() { // 平滑趋近目标角度 if (Mathf.Abs(Mathf.DeltaAngle(currentAngle, targetAngle)) > 0.1f) { // 计算最短旋转方向,Mathf.DeltaAngle自动处理360度循环 float angleDiff = Mathf.DeltaAngle(currentAngle, targetAngle); float step = rotationSpeed * Time.deltaTime * Mathf.Sign(angleDiff); currentAngle += step; currentAngle = Mathf.Repeat(currentAngle, 360f); // 若正北对应Y/Z轴,修改此处旋转轴(例如Quaternion.Euler(0, currentAngle, 0)) aircraft.transform.rotation = Quaternion.Euler(currentAngle, 0, 0); } } // 从输入框设置目标角度 public void SetTargetAngleFromInput() { if (float.TryParse(angleInput.text, out float inputAngle)) { targetAngle = Mathf.Repeat(inputAngle, 360f); } } // 单次左转90度(按钮点击调用) public void RotateLeftOnce() { targetAngle = Mathf.Repeat(targetAngle - 90f, 360f); } // 单次右转90度(按钮点击调用) public void RotateRightOnce() { targetAngle = Mathf.Repeat(targetAngle + 90f, 360f); } }
2. 简化按钮脚本
左侧按钮脚本(Left.cs)
using UnityEngine; using UnityEngine.EventSystems; public class Left : MonoBehaviour, IPointerDownHandler, IPointerUpHandler { public AircraftRotationController rotationController; private bool isPressed = false; private float holdRotationRate = 5f; // 按住时的旋转速率系数 void Update() { if (isPressed) { // 按住时持续调整目标角度 rotationController.targetAngle = Mathf.Repeat( rotationController.targetAngle - holdRotationRate * Time.deltaTime * rotationController.rotationSpeed, 360f ); } } public void OnPointerDown(PointerEventData eventData) => isPressed = true; public void OnPointerUp(PointerEventData eventData) => isPressed = false; }
右侧按钮脚本(Right.cs)
using UnityEngine; using UnityEngine.EventSystems; public class Right : MonoBehaviour, IPointerDownHandler, IPointerUpHandler { public AircraftRotationController rotationController; private bool isPressed = false; private float holdRotationRate = 5f; void Update() { if (isPressed) { rotationController.targetAngle = Mathf.Repeat( rotationController.targetAngle + holdRotationRate * Time.deltaTime * rotationController.rotationSpeed, 360f ); } } public void OnPointerDown(PointerEventData eventData) => isPressed = true; public void OnPointerUp(PointerEventData eventData) => isPressed = false; }
3. 场景配置步骤
- 创建一个
InputField用于输入角度,添加按钮并绑定AircraftRotationController的SetTargetAngleFromInput方法 - 将
AircraftRotationController挂载到空物体上,指定aircraft、angleInput,调整rotationSpeed参数 - 给左右按钮分别挂载对应的脚本,指定
rotationController引用 - 若你的正北方向对应世界空间Y轴或Z轴,修改核心脚本中
aircraft.transform.rotation的旋转轴参数
内容的提问来源于stack exchange,提问作者rpmx13
相关产品推荐
相关产品推荐

