Greetings!
I am making my first game and i want when my Player “Ball”/“Sphere” is moving and then audioclip.play(but not in c# .)
But i just need If reigidbody.velocity.magnitude = new vector 0,0,0 stop sound only play if velocity is > 0,0,0 . But it doesn’t work in script (It is showing me error ) . I just don’t want to depend on keys audio because if ball stuck somewhere then player press audio and it looks a dumb game if the ball is not moving and they can hear audio .
Please help me , Thanks .
Please guys suggest me something , I can’t able to done it .
can you post the part of the script youve tried, and the error please.
sure
here is
the code …
- using UnityEngine;
- using System.Collections;
-
- public class MoveSFX : MonoBehaviour {
- public AudioSource move;
- // Use this for initialization
- void Start () {
-
- }
-
- // Update is called once per frame
- void Update () {
- if(Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow))
-
- {
- if(rigidbody.velocity.magnitude > new Vector3(0,0,0))
- {
- move.Play();
- }
- }
- }
- }
Error …
-
- Assets/Scripts/MoveSFX.cs(16,39): error CS0019: Operator
>' cannot be applied to operands of typefloat’ and `UnityEngine.Vector3’
- Assets/Scripts/MoveSFX.cs(16,39): error CS0019: Operator
I also want to loop it until ball moves ,but how can i do that .
If i add
- move.loop;
then how can i Play that and the error also is very interesting .
That error is caused because you are comparing a Vector3 with a float… to avoid that error just change “new Vector3(0,0,0)” to “0”.
Also, your AudioSource MUST have an AudioClip assigned to it to play.
That’s it thanks it working but not looping as i thought with player long movement.I think adding this method also doesn’t work if i even make it biiger(ball still run sound onetime if i press key even if it stuck somewere)
- if(rigidbody.velocity.magnitude > 0.1)
- {
- move.Play();
- }
Please help me .
I got that by increasing value .
-
- if(rigidbody.velocity.magnitude > 1)
- {
- move.Play();
- }
But still can’t able to loop it .
Is that good if i change it to key instead of keydown , So with this it will recognize automatically when player velocity is greater then play sound ,
-
- if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
-
- {
- if(rigidbody.velocity.magnitude > 2)
- {
- move.Play();
- }
- }
But it’s not working , It only works if ball collide somewhere but on collide not on moving . It plays sound
Please suggest me about looping until player stops and making key that recognize itself is the velocity is greater if yes then play sound .
Thanks
you could try something like this, also ensure that in the inspector of your audiosource component that you have the loop audio enabled.
if(Input.GetKey (KeyCode.W) || Input.GetKey(KeyCode.UpArrow) )
{
if(rigidbody.velocity.magnitude >= 2f)
{
if(!move.isPlaying)
move.Play ();
}
}
else
{
move.Stop();
}
you will need to find the ‘isPlaying’ bool of the audiosource. as if you continually say move.Play() it will constantly be trying to restart the audio from the beginning.
see how you get on with that. hope it helps a bit.
That’s correct. Also, to stop it you should use something like this:
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
if (rigidbody.velocity.magnitude >= 2f)
{
if (!move.isPlaying)
move.Play();
}
}
else if (move.isPlaying)
{
move.Stop();
}
Here’s a handy little noise-versus-speed calculator I just threw together.
I’ll upload the entire project here, just unpack it somewhere and open it in Unity.
The heavy lifting is in taking the magnitude of the rigidbody velocity and using it to set the volume.
1913924–123466–forum289752.zip (214 KB)
Ooop, not sure, might have uploaded the wrong script file. REPLACE the script inthe above zipfile with the attached C# script here. I’ll update the zipfile tomorrow…
1913928–123467–NoiseVersusSpeed.cs (813 Bytes)
Thank-you very much guys for your great help , I am very happy that it works . Thank-you so much .
Th
The script is really awesome (Very detailed)
Thanks for that ,I learned so many ideas and more logic from that .
Thanks for help , I have another (last) confusion about this .
First i make my script more realistic so unity will auto recognize the magnitude
-
- using UnityEngine;
- using System.Collections;
-
- public class MoveSFX : MonoBehaviour {
- public AudioSource move;
- // Use this for initialization
- void Start () {
-
- }
-
- // Update is called once per frame
- void Update () {
-
- if(rigidbody.velocity.magnitude > 2)
- {
- move.Play();
- move.volume = 0.01f;
-
- }
-
- else
- move.Stop();
- }
- }
But when sound play then it start on each frame with loop(Very irritating) . Please tell me how can i make it smooth.
Please someone tell me to make it smooth.
I am still a noob .
please see my post above.
Where I mentioned you should check to see if the audio is playing or not using the if(!move.isPlaying), as this is the behaviour I mentioned when it restarts every frame.
you have removed all that code from your latest post.
Sorry about that i didn’t recognize that line logic , I will try it thanks For the help .
awesome - awesome - awesome . Thanks for this help . It work perfectly .