Unity2D角色随鼠标位置旋转异常问题求助
Hey there, let’s break down those frustrating rotation bugs you’re hitting— I’ve dealt with exactly these headaches when building similar snake-style games, so let’s work through each problem one by one:
1. Rotation is Way Too Slow or Fast
This almost always boils down to mixing up radians and degrees, or forgetting to tie rotation speed to frame rate.
Here’s the fix: Use Mathf.Atan2 to get the radian angle, convert it to Unity’s degree system, and use Time.deltaTime to make speed consistent across all frame rates:
// Get mouse position in world space (match the character's Z-axis!) Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); mouseWorldPos.z = transform.position.z; // Keep us on the same 2D plane // Calculate direction from character to mouse Vector3 direction = mouseWorldPos - transform.position; // Convert radian angle to degrees (Unity uses degrees for rotation) float targetAngle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg; // Smooth rotation (adjust the 5f value to tweak speed) transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, 0, targetAngle), 5f * Time.deltaTime);
Quick checks if speed is still off:
- Did you forget to multiply by
Mathf.Rad2Deg? That’ll make rotation super slow. - Are you running rotation logic in
FixedUpdateinstead ofUpdate? FixedUpdate runs at a fixed rate, which can cause speed inconsistencies.
2. Character Stuck Rotating Up/Down Only
This happens when you’re not locking the X and Y rotation axes— in 2D games, we only care about rotating around the Z-axis.
Force the X/Y axes to stay at 0 when setting rotation:
// After calculating targetAngle, set rotation directly with locked axes transform.rotation = Quaternion.Euler(0, 0, targetAngle);
Also, double-check your character’s initial rotation in the Inspector— reset it to (0, 0, 0) if it’s been messed up, and make sure no other scripts are modifying the X/Y rotation values.
3. Character Rotates But Doesn’t Face the Mouse (Wrong Axis)
This is a classic coordinate mix-up— either you’re using screen coordinates directly, or not aligning the mouse’s world position with your character’s plane.
The key steps to fix this:
- Always convert mouse screen position to world space, and match the character’s Z-axis value.
- Ignore the Z component of the direction vector to keep calculations in 2D.
- Only rotate around the Z-axis (the only rotation that matters for top-down 2D games).
Here’s a full, tested script you can drop in:
using UnityEngine; public class SnakeRotation : MonoBehaviour { [SerializeField] private float rotationSpeed = 6f; // Adjust in Inspector void Update() { // Convert mouse screen pos to world pos, matching character's Z depth Vector3 mouseScreenPos = Input.mousePosition; mouseScreenPos.z = Camera.main.WorldToScreenPoint(transform.position).z; Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(mouseScreenPos); // Calculate direction to mouse (ignore Z-axis) Vector3 lookDirection = mouseWorldPos - transform.position; lookDirection.z = 0f; // Only rotate if mouse is far enough away (avoid jitter when overlapping) if (lookDirection.magnitude > 0.1f) { float targetAngle = Mathf.Atan2(lookDirection.y, lookDirection.x) * Mathf.Rad2Deg; Quaternion targetRotation = Quaternion.Euler(0, 0, targetAngle); transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime); } } }
Extra Troubleshooting Tips
- Make sure your camera is set to Orthographic (slither.io uses a top-down ortho camera)— perspective cameras will mess up screen-to-world coordinate conversion.
- Check if your character has a parent object with rotation— parent rotations will override or skew your child object’s rotation.
- Ditch complex Euler angle order setups— you don’t need them for 2D top-down rotation, just stick to Z-axis changes.
内容的提问来源于stack exchange,提问作者Michael Brennan

