Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Camera is lagging when I play.

Discussion in 'Scripting' started by Cheeseboat, Sep 15, 2020.

  1. Cheeseboat

    Cheeseboat

    Joined:
    Oct 2, 2019
    Posts:
    8
    Hello people! I'm new to unity and I wrote a script to look around with the camera, and it was working fine, then I started making a start menu, and I played it to see if it worked, and my camera would glitch/lag when i looked around. 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);
    }
    }

    Please help!
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Usually you want to move the camera in LateUpdate instead of Update to allow everything else in your scene to move before you move the camera. But with glitches/lag you should be checking the Profiler for the causes. If your game is working extra long on something else for a few frames, you can't fix the issue with the camera code.
     
    zMarionz likes this.
  3. Cheeseboat

    Cheeseboat

    Joined:
    Oct 2, 2019
    Posts:
    8
    Okay, thanks, I'll take a look at it's profiler.
     
  4. zMarionz

    zMarionz

    Joined:
    Jun 14, 2021
    Posts:
    1
    Yes, Thank you very much, this is what I needed, in Update camera rendering the scene with a small delay, because the camera renders before moving, with LateUpdate it first moves and then the camera renders.