Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Why is my game so choppy when I run it?

Discussion in 'Scripting' started by BetrayedPickle, Jun 30, 2020.

  1. BetrayedPickle

    BetrayedPickle

    Joined:
    Mar 30, 2019
    Posts:
    119
    It is literally the title. When I test play my game, the camera moves very choppily. I don't think it is the script though, because sometimes it runs perfectly normal.
     
    Last edited: Jul 1, 2020
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,894
    It could be any number of things. Low framerate, a script that doesn't account for the framerate, a rigidbody that is not using interpolation. Hard to tell without seeing your project and your scripts.

    Maybe if you share your camera movement script that would be a good place to start.
     
  3. BetrayedPickle

    BetrayedPickle

    Joined:
    Mar 30, 2019
    Posts:
    119
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Collections.Specialized;
    4. using UnityEngine;
    5.  
    6. public class MouseLook : MonoBehaviour
    7. {
    8.    public float mouseSensitivity = 100f;
    9.  
    10.     public Transform playerBody;
    11.  
    12.     float xRotation = 0f;
    13.    
    14.    
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         Cursor.lockState = CursorLockMode.Locked;
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    25.         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    26.  
    27.  
    28.         xRotation -= mouseY;
    29.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    30.  
    31.         transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    32.         playerBody.Rotate(Vector3.up * mouseX);
    33.     }
    34. }
     
  4. BetrayedPickle

    BetrayedPickle

    Joined:
    Mar 30, 2019
    Posts:
    119
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The first thing I'd do is open the Profiler to see what framerates you are getting. If frame rate is very low, or jumping all over the place, then you've got a performance issue to solve. The Profiler can also help identify where the issue is.

    If it isn't a performance issue, it may be a movement problem, but I don't see any issue in the posted script.
     
  6. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Is the camera by any chance following a player or a vehicle? I see it is, I think I know what you're describing and it's not 'lag' it's the camera following the absolute movement of the player. Use FixedUpdate rather than Update and this problem should be solved.

    As it so happens I'm looking at this very mechanic of having a third person camera as well.
     
  7. BetrayedPickle

    BetrayedPickle

    Joined:
    Mar 30, 2019
    Posts:
    119
    @Joe-Censored I checked my framerate in the profiler, I'm getting 100-200 fps. It has to be a camera problem.
     
  8. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,894
    Can you share some details about your scene? Which objects are parents of which? Which scripts are attached to what? Are there rigidbodies involved?

    Can you describe the choppiness more? If the camera isn't moving is the game still choppy? Is it related only to rotation of the camera or movement, or both? Could you record a video?
     
    Joe-Censored likes this.
  10. BetrayedPickle

    BetrayedPickle

    Joined:
    Mar 30, 2019
    Posts:
    119
    @PraetorBlue Well, the camera will basically whip a bit to the left or right. It happens most when I put my crosshair over an object. My camera is a parent of the cylinder (which is the body of your character) Here is a video. Before you watch the video, a thing to keep in mind is that I had to record the video about 20 times before I got it to act choppy. Another thing is, towards the end, my camera just flings towards the ground. Also, it generally is even worse than that.
     
  11. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Where in your code do you control looking up/down? I only see you applying left/right rotation. It looks like you're using both the X and Y mouse axis only for left/right, but in your video you see the camera snaps to look straight down.
     
  12. BetrayedPickle

    BetrayedPickle

    Joined:
    Mar 30, 2019
    Posts:
    119
    @Joe-Censored kind of embarrassed saying this but I followed a brackeys tutorial so your guess is as good as mine..
     
    Joe-Censored likes this.