- Home /
Vehicle help
I already asked this question before on another user, but i was kinda vague. My problem is is than i need a vehicle to make sounds. Plain and simple: i need a c# of java script file to tell the game to play a mp3 file called truck upon pressing w a s and d keys. I tried a guess at it:`#include truck.mp3 { if (Input.GetKeyDown("w")) { truck.mp3 = true; } if (input.GetKeyDown("w")) { truck.mp3 = false; }
i am posotive that is wrong, but it is my best shot lol. i tried it and it didn't work. also that's only for the w key. anyway can somebody help me?
Seriously, ins$$anonymous$$d of gessing like that, why don't you try reading the manual? This stuff is really, really basic- you should read through and absorb everything there is in the scripting reference, because there are code examples for everything you might want to do, and only come here when you have a question which isn't covered by that.
Aye, when it comes to program$$anonymous$$g, danger begins when you start guessing. :) It actually takes a lot of discipline to never make "guesses", especially when you become just a little experienced and code starts flowing more easily. But as a cardinal rule: You must never write code that you "guess" is right. You must always know exactly what something does, and preferably exactly WHEN it does it.
I would rather have a shoddy piece of code, poorly written and perfor$$anonymous$$g poorly, but written by $$anonymous$$E so I understand it, than a well-written, complex function that runs fast, but that I copy-pasted, so it might use mathematics I don't understand, or language features I've never heard of. Why? Because I can fix and improve something I understand. If something I do later on breaks the code I copy-pasted in, I'll be helpless to fix it.
Does that mean you should never take and use the code others have written? No, certainly not. But it means you should never take and use the code others have written WITHOUT UNDERSTANDING IT FIRST. That last part is key, because it's how all programmers learn; we study each other's code. :)
Answer by roamcel · Oct 27, 2011 at 08:55 AM
It's pretty simple:
add an audiosource to your truck instance
drag the truck audio clip into the audiosource's audioclip slot.
on 'input', use the audioclip property of the audiosource component to play, stop, etc.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Audio not playing [Fixed] 1 Answer
Wait for sound is played 2 Answers
play audio for few seconds on key hit 1 Answer
Sound on collision not working 1 Answer