Unity3D飞船方向与旋转控制问题求助(补充图示重发)
Hey there! Let's work through this ship rotation problem you're stuck on. From your description, you want the ship to shift to specific rotated states when holding left/right arrows, then snap back to its original orientation when you release the keys. Here's a straightforward, Unity-friendly implementation:
Step 1: Set Up Variables to Track State
First, we'll need to store the ship's original rotation so we can revert to it later, plus a variable to control how far the ship turns (tweak this value to match your diagram's angles):
// Stores the ship's starting rotation state private Quaternion originalShipRotation; // Adjust this angle to match how far you want the ship to turn left/right public float turnAngle = 25f;
Step 2: Save Initial Rotation
In the Start() method, capture the ship's starting rotation as soon as the game starts:
void Start() { originalShipRotation = transform.rotation; }
Step 3: Handle Input & Rotation Logic
Update your Update() method to handle the arrow key presses. We'll cover two options—pick whichever fits your game feel:
Option 1: Instant Rotation & Reset
This is the simplest approach, where the ship jumps directly to the rotated state when you hold a key, and jumps back when you release:
void Update() { // Keep your existing forward movement code transform.position += transform.forward * Time.deltaTime * 10f; // Left arrow: Rotate to left-facing state (circle 3) if (Input.GetKey(KeyCode.LeftArrow)) { // Rotate around world Y-axis by -turnAngle relative to original rotation transform.rotation = originalShipRotation * Quaternion.Euler(0, -turnAngle, 0); } // Right arrow: Rotate to right-facing state (circle 2) else if (Input.GetKey(KeyCode.RightArrow)) { // Rotate around world Y-axis by +turnAngle relative to original rotation transform.rotation = originalShipRotation * Quaternion.Euler(0, turnAngle, 0); } // No keys held: Revert to original state (circle 1) else { transform.rotation = originalShipRotation; } }
Option 2: Smooth Rotation & Reset
If you want a more polished, gradual movement (instead of instant snaps), use Quaternion.Lerp to interpolate between rotations:
void Update() { transform.position += transform.forward * Time.deltaTime * 10f; if (Input.GetKey(KeyCode.LeftArrow)) { Quaternion targetLeftRotation = originalShipRotation * Quaternion.Euler(0, -turnAngle, 0); // Smoothly rotate toward the left target (adjust the 8f to control speed) transform.rotation = Quaternion.Lerp(transform.rotation, targetLeftRotation, Time.deltaTime * 8f); } else if (Input.GetKey(KeyCode.RightArrow)) { Quaternion targetRightRotation = originalShipRotation * Quaternion.Euler(0, turnAngle, 0); // Smoothly rotate toward the right target transform.rotation = Quaternion.Lerp(transform.rotation, targetRightRotation, Time.deltaTime * 8f); } else { // Smoothly revert to original rotation transform.rotation = Quaternion.Lerp(transform.rotation, originalShipRotation, Time.deltaTime * 8f); } }
Quick Notes
- Adjust the rotation axis: If your ship tilts instead of turns (e.g., leaning left/right like a plane), swap the Euler values to rotate around the X-axis instead:
Quaternion.Euler(-turnAngle, 0, 0)for left tilt,Quaternion.Euler(turnAngle, 0, 0)for right tilt. - Tweak values: Mess with
turnAngleand the Lerp speed (the 8f in Option 2) to get exactly the movement you want from your diagram.
内容的提问来源于stack exchange,提问作者Faisalmirza Mirza

