- Home /
Project in play mode creates a lagspike when pressing/releasing a key
Hi, as the title sais, my problem is that while I´m in play mode, everytime I press a key on my keyboard, the project laggs for half a second. This also happens when releasing the key again, but not when holding it down. My guess is that it is caused by my Character Controller, but I´m not sure. Here is said PlayerController:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { [SerializeField] Transform playerCamera = null; [SerializeField] float mouseSens = 3.5f; [SerializeField] float speed = 6f; [SerializeField] float smoothTime = 0.2f; [SerializeField] float gravity = -9.81f; [SerializeField] float jumpHeight = 2f; [SerializeField] bool lockMouse = true; float camRotation = 0f; float veloY = 0f; Vector2 currDir = Vector2.zero; Vector2 currDirVelo = Vector2.zero; Vector2 currMouseDelta = Vector2.zero; Vector2 currMouseDeltaVelo = Vector2.zero; CharacterController controller = null; void Start() { controller = GetComponent<CharacterController>(); if(lockMouse) {Cursor.lockState = CursorLockMode.Locked;Cursor.visible = false;} } // Update is called once per frame void Update() { UpdateLook(); UpdateMove(); } void UpdateLook() { Vector2 targetMouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); currMouseDelta = Vector2.SmoothDamp(currMouseDelta, targetMouseDelta, ref currMouseDeltaVelo, smoothTime/10f); camRotation -= currMouseDelta.y * mouseSens; camRotation = Mathf.Clamp(camRotation, -90f, 90f); playerCamera.localEulerAngles = Vector3.right * camRotation; transform.Rotate(Vector3.up * currMouseDelta.x * mouseSens); } void UpdateMove() { Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); targetDir.Normalize(); currDir = Vector2.SmoothDamp(currDir, targetDir, ref currDirVelo, smoothTime); if(controller.isGrounded) {veloY = 0f;} if(Input.GetButtonDown("Jump") && controller.isGrounded) { veloY = Mathf.Sqrt(jumpHeight * -2f * gravity); } veloY += gravity * Time.deltaTime; Vector3 velocity = (transform.forward * currDir.y + transform.right * currDir.x) * speed + Vector3.up * veloY; controller.Move(velocity * Time.deltaTime); } }
One weard thing to me is that these lag spikes do not occur in other projects which use a similar way to get an input to move a player. Here is one example of a project that did not cause any lagspikes to me.
I´m using Unity 2020.3.25f1
Any help is appreciated, even if it does not help ;)
Answer by PercyCreeper123 · Jan 19 at 12:46 PM
Sorry for the messed up look of the code... It looked normal for me before publishing my question...
Your answer
Follow this Question
Related Questions
Why does holding down any button cause ~20-30 FPS loss? 1 Answer
Simulate a "Bad Connection" 1 Answer
What should I do to prevent crashing? 0 Answers
Mouse input lag causes line rendering jitter 1 Answer
my android game lag sometimes 0 Answers