Script for fixed camera ( I'm new in programing)

Hello, I’m new to programing and I’m trying to script a very concrete type of camera.

I need a fixed camera in the z axis that rotates on his axis. That can be moved with WASD only in the X and Y.
With a limiter on the movement on X and Y.

Thanks to everyone that is willing to give any davice.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class CameraRotateScript : MonoBehaviour
{
    [Header ("Camera Settings")]
    public float CameraRotation;
    public float CameraSpeed;

    private float x;
    private float y;

    public float sensitivity;
    private Vector3 rotate;

    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
       

        y = Input.GetAxis("Mouse X");
        x = Input.GetAxis("Mouse Y");
        rotate = new Vector3(x, y * sensitivity, 0);
        transform.eulerAngles = transform.eulerAngles - rotate;

    

    }

Unity has Cinemachine. It’s pretty easy to lock an axis with it, keep it confined to an area, and so on. I would say it’s even less of a learning curve than doing this by scripting because you can experiment at runtime with all the different settings, eg you get instant feedback.

2 Likes

+1 for Cinemachine… seriously! Especially for new programmers.

Camera stuff is pretty tricky… you may wish to consider using Cinemachine from the Unity Package Manager.

There’s even a dedicated forum: Unity Engine - Unity Discussions

If you insist on making your own camera controller, the simplest way to do it is to think in terms of two Vector3 points in space: where the camera is LOCATED and where the camera is LOOKING.

private Vector3 WhereMyCameraIsLocated;
private Vector3 WhatMyCameraIsLookingAt;

void LateUpdate()
{
  cam.transform.position = WhereMyCameraIsLocated;
  cam.transform.LookAt( WhatMyCameraIsLookingAt);
}

Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations.