why does my game lags?
Hello Community, got this 2d plataformer, and the character is a vehicule, so Im using addforce to move it. i could swear it was working fine. but I dont know what happened now it lags when I press the horizontal axis keys. starts fine, after some seconds starts to lag untill it freezes, if I release the movement keys the game returns to be fine, but as soon as I press the either key (left or right) again it lags. If I just move by pressing the boost button the game is totaly fine. If I press the boost button when using the movement keys, the character just flys of the screen super fast like it had stored force in it. help please, I have modified the script several ways and couldnt fix it. this is the C# code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerStouss : MonoBehaviour {
 public float speed = 6f , maxVelocity = 8f;
 private Rigidbody2D myBody;
 private Animator anim;
 private float volLowRange = 0.5f;
 private float volHighRange = 1.0f;
 void Awake () {
     myBody = GetComponent<Rigidbody2D> ();
     anim = GetComponent<Animator> ();
 }
 // Use this for initialization
 void Start () {
 }
 
 // Update is called once per frame
 void Update () {
     
 }
 void FixedUpdate(){
     // this gets the imput of the player to jump
     PlayerMoveKeyboard ();{
     if (Input.GetButtonDown ("Jump"))
         Nitrous ();
         }
 }
     // this gets the imput for movement
 void PlayerMoveKeyboard(){
     float forceX = 0f;
     float vel = Mathf.Abs (myBody.velocity.x);
     float h = Input.GetAxisRaw ("Horizontal");
 
     if (h > 0) {
         if (vel < maxVelocity)
             forceX = speed;
         Vector3 temp = transform.localScale;
         temp.x = 1f;
             transform.localScale =temp;
                     
         anim.SetBool ("StoussMoving", true);
         SoundManagerScript.PlaySound("StoussMoving");
                 }
     else if (h < 0) {
         if (vel < maxVelocity)
             forceX = -speed;
         Vector3 temp = transform.localScale;
         temp.x = -1f;
             transform.localScale = temp;
         
         anim.SetBool ("StoussMoving", true);
         SoundManagerScript.PlaySound("StoussMoving");
     } 
     else
         anim.SetBool ("StoussMoving", false);
         myBody.AddForce (new Vector2 (forceX, 0));
     }
 void Nitrous () {
     
     SoundManagerScript.PlaySound("StoussBoost");
     if (transform.localScale.x > 0)
         myBody.AddForce (transform.right * 8, ForceMode2D.Impulse);
     
         //myBody.AddForce (transform.up * 1 , ForceMode2D.Impulse);
     
      if (transform.localScale.x < 0)
         myBody.AddForce (transform.right * -8, ForceMode2D.Impulse);
                 //myBody.AddForce (transform.up * 1 , ForceMode2D.Impulse);
 }
     
}
thanks.
guys I found Out that the culprit was this part :
Sound$$anonymous$$anagerScript.PlaySound("Stouss$$anonymous$$oving")
now I dont know how to fix it. is it that that sound is to heavy? Am I implementing it in a bad way?
Your answer
 
 
             Follow this Question
Related Questions
Ball Speed is not increasing as per code 0 Answers
why does my 2d platformer lags? 1 Answer
How to stop a sphere that is using GetAxis from moving? (Roll-A-Ball Tutorial) 0 Answers
How can we control two player objects at one time with RayCasting? 1 Answer
How to control the speed of AddForce (ForceMode.Impulse)????? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                