Unity相机控制脚本报错:无法将bool转换为string及功能实现问题
Hey there! Let's get your camera script sorted out—first fixing that error, then adding the reset-on-key-release functionality you want.
1. Fixing the "Cannot convert from bool to string" Error
The error comes from this line in your Update() method:
if (Input.GetKey(KeyCode.LeftArrow !=null)) {
Here's what's wrong:
KeyCode.LeftArrowis an enum (a value type), so it can never benull—that comparison doesn't make sense.- Worse,
KeyCode.LeftArrow != nullevaluates to a boolean value (true), butInput.GetKey()expects aKeyCodeas its parameter. This type mismatch is exactly what the error is complaining about.
This line seems like you were trying to detect when you let go of the arrow keys to reset the camera. We'll fix that as part of implementing the reset feature.
2. Implementing Camera Reset on Arrow Key Release
To make the camera smoothly reset to its original position when you let go of all arrow keys, we need to:
- Track the initial camera offset so we know where to reset to
- Detect when no arrow keys are being held
- Smoothly lerp the camera back to the initial target position
Here's the full fixed and updated script:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CompleteCameraController : MonoBehaviour { public Transform Player; public float rotationSpeed = 10f; public Vector3 offset; private Vector3 _initialOffset; private bool _isRotating = false; private void Start() { // Save the initial offset so we can reset to it later _initialOffset = offset; // Make sure camera starts looking at the player transform.LookAt(Player); } private void Update() { _isRotating = false; // Handle left arrow rotation if (Input.GetKey(KeyCode.LeftArrow)){ transform.RotateAround(Player.position, Vector3.down, Time.deltaTime * 25); _isRotating = true; } // Handle right arrow rotation if (Input.GetKey(KeyCode.RightArrow)) { transform.RotateAround(Player.position, Vector3.up, Time.deltaTime * 25); _isRotating = true; } // Handle up arrow rotation if (Input.GetKey(KeyCode.UpArrow)) { transform.RotateAround(Player.position, Vector3.left, Time.deltaTime * 25); _isRotating = true; } // Handle down arrow rotation (added null check for Player) if (Player != null && Input.GetKey(KeyCode.DownArrow)) { transform.RotateAround(Player.position, Vector3.right, Time.deltaTime * 25); _isRotating = true; } // If we're not rotating, reset the offset to initial value if (!_isRotating) { offset = Vector3.Lerp(offset, _initialOffset, rotationSpeed * Time.deltaTime); } // Always keep the camera looking at the player transform.LookAt(Player); } void LateUpdate() { if (Player == null) return; // Calculate target position based on current offset Vector3 desiredPosition = Player.position + offset; // Smoothly move to the target position Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, rotationSpeed * Time.deltaTime); transform.position = smoothedPosition; } }
Key Changes Explained:
- Initial Offset Tracking: We added
_initialOffsetto store the original camera position relative to the player, so we have a reference point to reset to. - Rotation State: The
_isRotatingboolean tracks if any arrow key is being held. If not, we smoothly lerp theoffsetback to its initial value. - Fixed Error Line: Removed the broken
Input.GetKey(KeyCode.LeftArrow !=null)line and replaced it with a clean state check for rotation. - Continuous Look: The camera will always face the player, even during rotation and reset.
- Null Safety: Added a check for
PlayerinLateUpdateto avoid errors if the player reference is missing.
This script will let you rotate the camera around the player with arrow keys, and when you release all keys, the camera will smoothly glide back to its original position relative to the player.
内容的提问来源于stack exchange,提问作者Kingwebbie

