Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

First person movement code error

I am currently doing the mouse look script and have finished by my character can only look up and down. I am sheiße at coding so be gentle. Here is my code.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class MouseLook : MonoBehaviour

{



  public float mouseSensitivity = 100f;


  public Transform playerBody;


  float xRotation = 0f;


  // Start is called before the first frame update

  void Start()

  {

    Cursor.lockState = CursorLockMode.Locked;

  }


  // Update is called once per frame

  void Update()

  {

    float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;

    float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;


    xRotation -= mouseY;

    xRotation = Mathf.Clamp(xRotation, -90f, 90f);


    transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);

    playerBody.Rotate(Vector3.up * mouseX);

  }

}

Answers

  • try this:

    using UnityEngine;


    public class cam : MonoBehaviour

    {

        public float ms;

        public Transform player;

        public float mr;

        public float angle = 20;


        private void Start()

        {

           

            Cursor.lockState = CursorLockMode.Locked;

        }

        private void Update()

        {

            float mx = Input.GetAxis("Mouse X") * ms * Time.deltaTime;

            float my = Input.GetAxis("Mouse Y") ;

           

            player.Rotate(Vector3.up,mx);

            mr -= my;

            mr = Mathf.Clamp(mr, -angle, angle);

            transform.localRotation = Quaternion.Euler(mr, 0, 0);

        }

    }

Sign In or Register to comment.