Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by kiwi438 · Dec 15, 2021 at 12:06 AM · sound effects

Using Object Rotation For Changing the pitch of Sound

Hello! I'm new to Unity and especially for coding but I'm interested in how to realise that the pitch of a sound changes by the rotating of a cube for example.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by metalted · Dec 15, 2021 at 06:55 AM

You might want to check out the following page: https://docs.unity3d.com/ScriptReference/AudioSource-pitch.html . You can alter the example scripts OnGUI function to fit your needs.


We then look up how to get the angle from a gameobject, like on this page: https://answers.unity.com/questions/49095/get-object-rotation-angle.html .


Last thing would be to scale the rotation value (0-180) to the low and high pitch values. This is just a simple formula, which you can find here: https://stats.stackexchange.com/questions/281162/scale-a-number-between-a-range.


Finally we combine everything and end up with the following code:

 using UnityEngine;
 
 // A script that plays your chosen song.  The pitch starts at 1.0.
 // You can increase and decrease the pitch and hear the change
 // that is made.
 
 public class AudioExample : MonoBehaviour
 {
     public float pitchValue = 1.0f;
     public AudioClip mySong;
 
     private AudioSource audioSource;
     private float low = 0.75f;
     private float high = 1.25f;
 
     void Awake()
     {
         audioSource = GetComponent<AudioSource>();
         audioSource.clip = mySong;
         audioSource.loop = true;
     }
 
     void Update()
     {
         float angle = Quaternion.Angle(Quaternion.Euler(new Vector3(0,0,0)),transform.rotation); 
         float pitchValue = (angle / 180f) * (high - low) + low;
         audioSource.pitch = pitchValue;
     }
 }

The pitch will be highest at 180 degrees, lowest at 0.

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
avatar image
1

Answer by Zaeran · Dec 15, 2021 at 06:44 AM

Using a Gameobject variable named 'cube' as an example:

 GetComponent<AudioSource>().pitch = cube.transform.eulerAngles.x / 360;


This will set the pitch from 0 - 1 based on the x axis rotation of the cube. You can play with the numbers a little to get the pitch higher than 1, such as dividing by 180 to get a value between 0 - 2

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

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.

Follow this Question

Answers Answers and Comments

131 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

Related Questions

TOD script editing (sounds) 0 Answers

How to make player death sound effect / hitsound play once when function is called? 1 Answer

Rigidbody not colliding...? or is my code wrong? 0 Answers

Optimised audio system design for mobile 0 Answers

OnAudioFilterRead issues on Android? 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