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 Mar 27, 2019 at 06:30 PM by Brentos13 for the following reason:

Other

avatar image
0
Question by Brentos13 · Mar 26, 2019 at 01:55 AM · scripting problemaudiosourcepitch

How to correctly add audio to rolling ball

Hi,

i've been messing with this for a week and have had no success. I want to add sound to the ball, and when it picks up speed for the audio to speed up. I know i would use Pitch for this, but i'm not sure how to correctly incorporate it into my script. I tried adding a second audio source, but to no avail. I was able to get the "clank" (_audio1 in script) sound when the object hits walls, but that's a bout it. Any help would be greatly appreciated as there's very little material on how to do this. Below is script in C#

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class NewBehaviourScript : MonoBehaviour
 {
     AudioSource _audio;
     [SerializeField]
     AudioClip _clip;
 
     AudioSource _audio2;
     [SerializeField]
     AudioClip _clip2;
 
 
     [SerializeField]
     Rigidbody _rigidBody;
 
     float _nominalSpeed;
 
 
     private void Start()
     {
         _audio = GetComponent<AudioSource>();
 
         _audio2 = GetComponent<AudioSource>();
 
         _rigidBody = GetComponent<Rigidbody>();
 
         //_Play = true;
     }
 
     private void Update()
     {
         _nominalSpeed = _rigidBody.velocity.magnitude;
 
         //_audio.pitch = _rigidBody.velocity.magnitude / _nominalSpeed;
     }
 
     private void OnCollisionEnter(Collision collision)
     {
         if (collision.gameObject.tag == "Ground" && _nominalSpeed >= 0.1)
 
             _audio.Play();
 
         _audio2.Play(); 
     
     
 
         else if (collision.gameObject.tag == "Ground" && _nominalSpeed < 0.1)
 
 
             _audio.Pause();
     }
 } 
Comment
Add comment · Show 1
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
avatar image Brentos13 · Mar 25, 2019 at 08:50 PM 0
Share

I can't seem to get the code to fit, I tried code tags.

1 Reply

  • Sort: 
avatar image
0

Answer by Vollmondum · Mar 26, 2019 at 06:07 AM

  1. To start with, don't you EVER use _ sign.

  2. Then, [SerializeField] is something you don't just stick here and there. Read about when and why.

  3. Read what's public and private (and protected and static) and why and when it's used.

      using System;
         using System.Collections;
         using System.Collections.Generic;
         using UnityEngine;
         public class myBalls: MonoBehaviour
         {
             public AudioSource ballsAudioSource;
             public AudioClip ballsSound;
         
             public float defaultSpeed;
             public float currentSpeed;
             public float defaultPich;
         
             void Start()
             {
                 ballsAudioSource = GetComponent<AudioSource>();
             }
         
             void Update()
             {
                 ballsAudioSource.pitch = defaultPitch * (currentSpeed / defaultSpeed);
             }
         
             void OnCollisionEnter(Collision col)
             {
                 if(col.gameObject.tag == "Ground" && GetComponent<RigidBody>().velocity > 0)
                 {
                     if(!ballsAudioSource.isPlaying())
                     {
                         ballsAudioSource.Play();
                     }
                 }
                 else
                 {
                     ballsAudioSource.Stop();
                 }
             }
         }
    
    

Not sure if it'll compile, modify whatever lines as needed. Gave you main logic.

Comment
Add comment · Show 1 · 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
avatar image Brentos13 · Mar 27, 2019 at 06:30 PM 0
Share

lol why can't I EVER use this? In a class I was taking they said it was good practice to use. Oh well, I figured it out, but thanks for the comment.

Follow this Question

Answers Answers and Comments

179 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 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

does unity 5 pitch shifter audio effect change playback speed? 1 Answer

How Can I Toggle External Audio Sources? 0 Answers

Affecting all audio with slow motion but can't reset to original pitc 2 Answers

Gear sound changes smoothed! 0 Answers

audio doesn't play on movement 0 Answers


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