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 /
avatar image
0
Question by ogito123 · Jun 22, 2019 at 03:33 PM · prefabassetaudiosource

Adding audio to asset bugs out the movement

Im trying to learn Unity3d from a tutorial. I have a rocket prefab where the rocket moves up when I click the space button, and in the tutorial next step is to put the audio source. But when I put the audio source, without changing anything else, the movement doesn't work as well. As if the object became heavier (it works if i decrease the mass of my rigidbody). Why could this be and how can I fix this?

This is the code i have. Doesn't matter if this code is added or not the problem persist if i comment it out too.

if (Input.GetKey(KeyCode.Space)) { rigidBody.AddRelativeForce(Vector3.up); if (!audioSource.isPlaying) { audioSource.Play(); } } else { audioSource.Stop(); } Expected result is the speed of my rocket doesnt change. But it changes(slows down/ doesnt fly at all), Im trying to learn Unity3d from a tutorial. I have a rocket prefab where the rocket moves up when I click the space button, and in the tutorial next step is to put the audio source. But when I put the audio source, without changing anything else, the movement doesn't work as well. As if the object became heavier (it works if i decrease the mass of my rigidbody). Why could this be and how can I fix this?

This is the code i have. Doesn't matter if this code is added or not the problem persist if i comment it out too.

if (Input.GetKey(KeyCode.Space)) { rigidBody.AddRelativeForce(Vector3.up); if (!audioSource.isPlaying) { audioSource.Play(); } } else { audioSource.Stop(); } Expected result is the speed of my rocket doesnt change. But it changes(slows down/ doesnt fly at all)

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

· Add your reply
  • Sort: 
avatar image
0

Answer by Epsilon_Delta · Jun 22, 2019 at 05:36 PM

It is hard to tell without the whole code (next time please include all the relevant code, like in what function is your code embedded (Update / FixedUpdate ...)). But my guess is, that your code is in Update() and is framerate dependent. Adding audiosource can cause framerate drop and thus less force applied to your rocket. Moreover, I think you may have wrongly placed brackets: your "else" statement executes when you are not pressing space. Try this:

 if (Input.GetKey(KeyCode.Space)) 
 {
  rigidBody.AddRelativeForce(100*Vector3.up*Time.deltaTime);
  if (!audioSource.isPlaying)
  {
    audioSource.Play();
  } 
  else 
  { 
  audioSource.Stop(); 
  }
 } 

I multiplied the force by 100, because Time.deltaTime tends to be very small, but you have to try what's the best value. Best create public variable "strength", multiply force by strength and adjust it in the inspector. Other option is you move your code to FixedUpdate() function, but only force application part, not the input detection. I guess this is a little bit complicated option for you right now. (And by the way I probably started learning Unity by the same tutorial and I spotted this same "mistake" they've made. It's not really a mistake, it is there for simplicity reasons).

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

139 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

Related Questions

Getting a prefab with javascript FindAsset (Solved) 0 Answers

Creating prefab from instance of a GameObject loses materials 1 Answer

Modern Asset Handling - Multiple Projects? 0 Answers

Calendar not showing when installing from apk 0 Answers

Usable items - How can I create an asset, that could reference gameObjects that live in separate scenes? 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