Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
This question was closed Aug 03, 2017 at 08:06 PM by DeveloperDB for the following reason:

Other

avatar image
0
Question by DeveloperDB · Aug 03, 2017 at 08:11 AM · movementsoundaudiosourcetags

I want to add a landing sound effect when my character lands on a platform, is there somebody that can help me?

I've already added a lot of sound effects in this and other scripts, but I'm not able to figure out how to add a landing sound effect when the player lands on one of the platforms. (Note: I've already added a variable 'landingSound'.) This is the current script:

 public float moveSpeed;
 private float moveSpeedSafe;
 public float moveSpeedMultiplier;

 public float speedIncreasePoint;
 private float speedIncreacePointSafe;

 public float speedPointMultiplier;

 private float speedPointCount;
 private float speedPointCountSafe;

 public float jumpForce;

 public float jumpTime;
 private float jumpTimeCounter;

 private bool stoppedJumping; 

 private Rigidbody2D myRigidbody;

 private bool onPlatform;
 public LayerMask whatIsPlatform;
 public Transform platformChecker;
 public float platformCheckRadius;

 /*private Collider2D myCollider;*/

 private Animator myAnimator;

 public gameManager theGameManager;

 public AudioSource jumpSound;
 public AudioSource deathSound;

 public AudioSource landingSound;

 void Start()
 {
     myRigidbody = GetComponent<Rigidbody2D>();
     /*myCollider = GetComponent<Collider2D>();*/
     myAnimator = GetComponent<Animator>();

     jumpTimeCounter = jumpTime;

     speedPointCount = speedIncreasePoint;

     moveSpeedSafe = moveSpeed;
     speedPointCountSafe = speedPointCount;
     speedIncreacePointSafe = speedIncreasePoint;

     stoppedJumping = true;
 }

 void Update()
 {
     /*onPlatform = Physics2D.IsTouchingLayers(myCollider, whatIsPlatform);*/

     onPlatform = Physics2D.OverlapCircle(platformChecker.position, platformCheckRadius, whatIsPlatform);

     if(transform.position.x > speedPointCount)
     {
         speedPointCount += speedIncreasePoint;
         speedIncreasePoint = speedIncreasePoint * speedPointMultiplier;
         moveSpeed = moveSpeed * moveSpeedMultiplier;
     }

     myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);

     if (Input.GetKeyDown(KeyCode.Space))
     {
         if(onPlatform)
         {
             myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
             stoppedJumping = false;
             jumpSound.Play();
         }
     }

     if (Input.GetKey(KeyCode.Space) && !stoppedJumping)
     {
         if(jumpTimeCounter > 0)
         {
             myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
             jumpTimeCounter -= Time.deltaTime;
         }
     }

     if (Input.GetKeyUp(KeyCode.Space))
     {
         jumpTimeCounter = 0;
         stoppedJumping = true;
     }

     if (onPlatform)
     {
         jumpTimeCounter = jumpTime;
     }

     myAnimator.SetFloat("Speed", myRigidbody.velocity.x);
     myAnimator.SetBool("onPlatform", onPlatform);
 }

 void OnCollisionEnter2D(Collision2D other)
 {
     if(other.gameObject.tag == "deathFloor")
     {
         theGameManager.RestartGame();
         moveSpeed = moveSpeedSafe;
         speedPointCount = speedPointCountSafe;
         speedIncreasePoint = speedIncreacePointSafe;
         deathSound.Play();
     }
 }
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

1 Reply

  • Sort: 
avatar image
1

Answer by Timo326 · Aug 03, 2017 at 08:48 AM

You can add a bool that has the state of onPlatform in the frame before. If it changes play the sound.

 private bool onPlatformLastFrame = false;
     
 void Update()
  {
      /*onPlatform = Physics2D.IsTouchingLayers(myCollider, whatIsPlatform);*/
      onPlatform = Physics2D.OverlapCircle(platformChecker.position, platformCheckRadius, whatIsPlatform);
      if (onPlatform == true && onPlatformLastFrame == false)
          landingSound.Play();
      onPlatformLastFrame = onPlatform;
 
  ...




Comment
Add comment · Share
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

Follow this Question

Answers Answers and Comments

100 People are following this question.

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

Related Questions

Mac built-in microphone stopped sending data to Unity. 0 Answers

Audio issues.. please help! :) 3 Answers

Only able to play a single sound through AudioSource. 1 Answer

What could make the audio loop? 2 Answers

How to get spatial 3D sound on Video Player 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges