0
\$\begingroup\$

I have a plane, a first person camera, and a player controller in a scene. There's a weird stutter in the game preview even though there are not a lot of assets in the project, average FPS, and don't see lag on other heavier projects. I tried changing the asset and replacing with a cube, still see the lag.

Any idea what could be causing this? Game is not GPU bound.

Environment

  • Windows 10 NVIDIA GeForce RTX 3060
  • Intel(R) UHD Graphics
  • Unity 2021.3.14f1
  • DX 11

Steps to reproduce:

  1. Add plane + lighting settings (turned off lightbox and changed environment color to black)
  2. Add capsule
  3. Add player controller script
  4. Add first person camera script
  5. Add tree fbx
  6. Add audio source to tree fbx
  7. See visual stutter
  8. Tried deleting tree fbx and added cube to see if I
    still get the visual stutter - still there
  9. Tried deleting first person camera script - visual stutter still there

I think its a hardware issue because i see the visual stutter just moving assets in the game preview without running it. but i dont see it running other projects.

Player Controller script

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

public class PlayerController : MonoBehaviour
{
    // The speed at which the character moves
    public float moveSpeed = 5.0f;

    // Update is called once per frame
    void Update()
    {
        UpdatePlayer();
    }

    void UpdatePlayer()
    {
        // Get the input axis for horizontal movement
        float horizontalInput = Input.GetAxis("Horizontal");

        // Get the input axis for vertical movement
        float verticalInput = Input.GetAxis("Vertical");

        // Calculate the movement vector
        Vector3 movement = new Vector3(horizontalInput, 0, verticalInput) * moveSpeed * Time.deltaTime;

        // Move the character
        transform.position += movement;
    }
}

First Person Camera script

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

public class FirstPersonCamera : MonoBehaviour
{
    // Variables
    public Transform player;
    public float mouseSensitivity = 2f;
    float cameraVerticalRotation = 0f;

    bool lockedCursor = true;


    void Start()
    {
        // Lock and Hide the Cursor
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;

    }

    
    void Update()
    {
        // Collect Mouse Input

        float inputX = Input.GetAxis("Mouse X")*mouseSensitivity;
        float inputY = Input.GetAxis("Mouse Y")*mouseSensitivity;

        // Rotate the Camera around its local X axis

        cameraVerticalRotation -= inputY;
        cameraVerticalRotation = Mathf.Clamp(cameraVerticalRotation, -90f, 90f);
        transform.localEulerAngles = Vector3.right * cameraVerticalRotation;


        // Rotate the Player Object and the Camera around its Y axis

        player.Rotate(Vector3.up * inputX);
       
    }
}

enter image description here

visual stutter with stats

Edit Mode edit mode

Play Mode play mode

\$\endgroup\$
8
  • 1
    \$\begingroup\$ Does your profiler show anything? What all is happening in this scene - do you have any physics interactions or C# scripts running? Try to walk us through the steps needed to reproduce this problem, starting from a new, empty project. Once we can replicate the issue, we can isolate potential causes and test fixes to be sure they'll work for you. \$\endgroup\$
    – DMGregory
    Dec 19, 2022 at 3:16
  • \$\begingroup\$ I appreciate the pointers @DMGregory Updated my comment with steps and profiler screenshots above. \$\endgroup\$
    – jenkatan29
    Dec 22, 2022 at 16:25
  • \$\begingroup\$ The profiler graph is for you to find where the spikes are, then click on a spike to see what's taking up the time listed out below. The graph alone isn't much good to us, since we can't do the clicking for you. \$\endgroup\$
    – DMGregory
    Dec 22, 2022 at 16:30
  • \$\begingroup\$ @DMGregory added more screenshots and looks like editor loop is pretty high. Tried deleting all unnecessary packages, turning off Chrome in case it was hogging CPU, used standalone Profiler, but still no change. I'm currently watching the Unite 2020 Profiler presentation to get a better understanding, but sharing first in case you have any other pointers on where to look. Thanks again and happy new year \$\endgroup\$
    – jenkatan29
    Dec 30, 2022 at 11:36
  • \$\begingroup\$ Editor loop is not part of your game. It's the processing the editor needs to do to update the inspector pane, scene view, etc. So if this is the only source of stutter, you do not have a problem in your game. Try building a standalone executable and measuring that without the editor. \$\endgroup\$
    – DMGregory
    Dec 30, 2022 at 13:51

1 Answer 1

0
\$\begingroup\$

I rebuilt the game in 2020.3.43f1 URP and the stutter stopped.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ This answer would be better if it identified the version you were using, and the version you upgraded to. \$\endgroup\$
    – DMGregory
    Jan 3, 2023 at 15:22
  • 1
    \$\begingroup\$ One thing to bear in mind... If you haven't set the script execution order, UpdatePlayer() may be being called either before or after the camera movement. I would've suggested moving camera movement from Update() to LateUpdate() to make it consistent. If that was the root cause, it might be pure chance your new project is working. \$\endgroup\$
    – Basic
    Jan 3, 2023 at 16:27
  • \$\begingroup\$ Remember to click the checkmark icon on the left to mark this answer as "Accepted" if it solved the problem. \$\endgroup\$
    – DMGregory
    Jan 4, 2023 at 17:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .