#need help with camera movement

1 messages · Page 1 of 1 (latest)

silver idol
#

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

public class MouseLook : MonoBehaviour
{
public float sensitivity = 100f;

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

// Update is called once per frame
void Update()
{
    //getting the inputs

    float mouseX = Input.GetAxis("Mouse X");
    float mouseY = Input.GetAxis("Mouse Y");

    float movementY = -mouseY * sensitivity * Time.deltaTime;
    float movementX = mouseX * sensitivity * Time.deltaTime;

    //actually rotating the object
    Vector3 rotation = new Vector3(movementY, movementX, 0);
    float clampedrotationX = rotation.x;
    clampedrotationX = Mathf.Clamp(clampedrotationX, -90, 90);
    rotation = new Vector3(clampedrotationX, movementX, 0);

    transform.Rotate(rotation);
}

}

This is my camera code, i think it should work but the camera is doing weird stuff.

light plinth
#

Consider whether or not you need to rotate your object on the x axis. I would normally (unless making a flying vehicle or submarine) parent the camera under the character, then set the character Y rotation accordingly, and only change the X axis for the camera.

Otherwise, here is a really good source for mouse movement I've found before:

https://forum.unity.com/threads/a-free-simple-smooth-mouselook.73117/

silver idol
#

thank you