My game isn't smooth, it runs at 60 fps but has quick lag spikes just under every second. I really can't figure out why. I have a feeling it's to do with my player movement code.
public ParticleSystem particleLauncher;
public float playerSpeed;
public float jumpSpeed;
public float coolDownTime = 2;
public CameraShake cameraShake;
public bool dashEnabled;
public bool isCamShakeEnabled;
private bool isJumping;
private float move;
private Rigidbody2D rb;
private bool facingRight;
private float nextDashTime = 0;
public bool movementAllowed;
private Animator anim;
// Start is called before the first frame update
void Start()
{
isCamShakeEnabled = true;
Scene currentScene = SceneManager.GetActiveScene();
string sceneName = currentScene.name;
dashEnabled = true;
anim = GetComponent<Animator>();
facingRight = true;
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
if(movementAllowed == true)
if (Input.GetButtonDown("Jump") && !isJumping)
{
rb.AddForce(new Vector2(rb.velocity.x, jumpSpeed));
isJumping = true;
}
}
// Update is called once per frame
void Update()
{
Application.targetFrameRate = 60;
if (movementAllowed == true)
{
move = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(move * playerSpeed, rb.velocity.y);
if (isJumping == true)
{
anim.SetBool("isJumping", true);
}
if(isJumping == false)
{
anim.SetBool("isJumping", false);
}
Flip(move);
if (move == 0)
{
anim.SetBool("isRunning", false);
}
else
{
anim.SetBool("isRunning", true);
}
if (Time.time > nextDashTime)
{
if (Input.GetButtonDown("Dash") && dashEnabled == true)
{
if(SceneManager.GetActiveScene() == SceneManager.GetSceneByName("Level4-2"))
{
nextDashTime = Time.time + coolDownTime;
StartCoroutine("DashMove");
}
else
{
nextDashTime = Time.time + coolDownTime;
StartCoroutine("DashMove");
StartCoroutine(cameraShake.Shake(.10f, .3f));
}
}
}
}
if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName ("Level5-4"))
{
dashEnabled = false;
isCamShakeEnabled = false;
}
if (SceneManager.GetActiveScene() == SceneManager.GetSceneByName("Level5-4"))
{
isCamShakeEnabled = false;
}
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.transform.CompareTag("killer"))
gameObject.GetComponent<TrailRenderer>().enabled = false;
}
IEnumerator DashMove()
{
playerSpeed += 60;
particleLauncher.Emit(30);
yield return new WaitForSeconds(.07f);
playerSpeed -= 60;
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Ground"))
{
isJumping = false;
gameObject.GetComponent<TrailRenderer>().enabled = true;
}
if (other.gameObject.CompareTag("BK"))
{
isJumping = false;
}
if (other.gameObject.CompareTag("MGround"))
{
this.transform.parent = other.transform;
isJumping = false;
}
if (other.gameObject.CompareTag("Mcircle"))
{
this.transform.parent = other.transform;
isJumping = false;
}
}
void OnCollisionExit2D(Collision2D other)
{
if (other.gameObject.CompareTag("MGround"))
{
this.transform.parent = null;
isJumping = true;
}
if (other.gameObject.CompareTag("Mcircle"))
{
this.transform.parent = null;
isJumping = true;
}
if (other.gameObject.CompareTag("Ground"))
{
isJumping = true;
}
if (other.gameObject.CompareTag("BK"))
{
isJumping = false;
}
}
private void Flip(float move)
{
if (move > 0 && !facingRight || move < 0 && facingRight)
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}