Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In Unity, how to control the horizontal and vertical movement, as well as rotation and panning, of a top-down camera (45-degree angle perspective)?

I’m about to go crazy with this problem. I’ve spent two days and still can’t figure it out. I need a 3D camera control effect like the game ‘Frostpunk’. I don’t know how to use Euler angles or quaternions. The most important thing is that everything becomes unpredictable when the camera’s Rotation.Y changes. I need a formula to solve this problem or just give me a piece of code. Please, I beg of God.

I can now move correctly up, down, left, and right in world coordinates, but problems arise when the local coordinate system is different from the world coordinate system.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I understand your frustration , camera control can be tricky in 3D space.This script uses Euler angles to control the camera, so you don’t need to worry about quaternions.

`

public class CameraController : MonoBehaviour
{
    public Transform target;
    public float distance = 5.0f;
    public float height = 2.0f;
    public float rotationDamping = 3.0f;
    public float heightDamping = 2.0f;
    public float zoomDamping = 2.0f;
    public float minZoomDistance = 2.0f;
    public float maxZoomDistance = 10.0f;

    private void LateUpdate()
    {
        if (!target)
            return;

        float currentRotationAngle = transform.eulerAngles.y;
        float currentHeight = transform.position.y;

        float desiredRotationAngle = target.eulerAngles.y;
        float desiredHeight = target.position.y + height;

        currentHeight = Mathf.Lerp(currentHeight, desiredHeight, heightDamping * Time.deltaTime);
        currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, desiredRotationAngle, rotationDamping * Time.deltaTime);

        Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);

        float desiredDistance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * zoomDamping, minZoomDistance, maxZoomDistance);
        Vector3 zoomVector = new Vector3(0, 0, -desiredDistance);
        Vector3 position = target.position + (currentRotation * zoomVector);

        transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * zoomDamping);
        transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);

        transform.LookAt(target);
    }
}`

** You may use this script to attach it to the camera object in your scene

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading