• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by immodiumvash · May 29, 2017 at 06:29 AM · c#soundlagmanagerplayoneshot

my sound clip reference is lagging playmode

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 (); anim = GetComponent (); } // 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);

}

}

guys I found Out that the culprit was this part :

SoundManagerScript.PlaySound("StoussMoving")

now I dont know how to fix it. is it that that sound is to heavy (2.9megabites)? or Am I implementing it in a bad way? this is the soundmangaer code:

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

public class SoundManagerScript : MonoBehaviour {

 public static AudioClip StoussMoving, StoussShooting, StoussBoost, StoussBraking, StoussHit, StoussDeath;
 static AudioSource audioSrc;


 void Start () {
     StoussMoving = Resources.Load <AudioClip> ( "StoussMoving");
     StoussShooting = Resources.Load <AudioClip> ( "StoussShooting");
     StoussBoost = Resources.Load <AudioClip> ( "StoussBoost");
     StoussBraking = Resources.Load <AudioClip> ( "StoussBraking");
     StoussHit = Resources.Load <AudioClip> ( "StoussHit");
     StoussDeath = Resources.Load <AudioClip> ( "StoussDeath");

     audioSrc = GetComponent <AudioSource> ();
 }
 
 // Update is called once per frame
 void Update () {
     
 }

 public static void PlaySound (string clip){
 
     switch (clip) {

     case "StoussMoving":
         audioSrc.PlayOneShot (StoussMoving);
         break;

     case "StoussShooting":
         audioSrc.PlayOneShot (StoussShooting);
         break;

     case "StoussBoost":
         audioSrc.PlayOneShot (StoussBoost);
         break;
     case "StoussBraking":
         audioSrc.PlayOneShot (StoussBraking);
         break;
     case "StoussHit":
         audioSrc.PlayOneShot (StoussHit);
         break;

     case "StoussDeath":
         audioSrc.PlayOneShot (StoussDeath);
         break;
     }

 }


}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Trigger diferent sounds playing from the same object 1 Answer

Distribute terrain in zones 3 Answers

Audio effect on collision 2 Answers

how can mute all sounds in the Scene? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges