Unity3D HELP!!!!!!!!!!!!!!!!!!!!!! : How can i make physics controller jump/PLAY SOUND.

Hi,im making one serious thing in my fps game,anyway i have physics controller which i can controll game object: Ball
Well i need to make it jump and play sounds when ball start to jump (jumps up) and sound when is landed. And also when moving ball. (Footsteps)
Lol, i know it sounds easy,but i cant do that :((
Need sound when ball is moving (like footsteps) When starting jump-jumps up (Plays jump sound) and when is landed (Plays landing sound) Just like fps player controller sounds (In movement or action :D)
Please help me at this,im in real problem now,i cannot do anything further without it.
Please Please help me at this.

using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour
{
   private GameObject _GameManager;
   public Vector3 movement;
   public float moveSpeed = 6.0f;
   public float drag = 2;
   
   void Start()
   {
      _GameManager = GameObject.Find("_GameManager");
   }
   
   void Update ()
   {   
      Vector3 forward = Camera.main.transform.TransformDirection(Vector3.forward);
      forward.y = 0;
      forward = forward.normalized;
      
      Vector3 forwardForce = forward * Input.GetAxis("Vertical") * moveSpeed;
      rigidbody.AddForce(forwardForce);
      
      Vector3 right= Camera.main.transform.TransformDirection(Vector3.right);
      right.y = 0;
      right = right.normalized;

      Vector3 rightForce= right * Input.GetAxis("Horizontal") * moveSpeed;
      rigidbody.AddForce(rightForce);
   }
   
   void OnTriggerEnter  (Collider other  )
   {
      if (other.tag == "Destroy")
      {
         _GameManager.GetComponent<GameManager>().Death();
         Destroy(gameObject);
      }
      else if (other.tag == "Coin")
      {
         Destroy(other.gameObject);
         _GameManager.GetComponent<GameManager>().FoundCoin();
      }
      else if (other.tag == "SpeedBooster")
      {
         movement = new Vector3(0,0,0);
         _GameManager.GetComponent<GameManager>().SpeedBooster();
      }
      else if (other.tag == "JumpBooster")
      {
         movement = new Vector3(0,0,0);
         _GameManager.GetComponent<GameManager>().JumpBooster();
      }
      else if (other.tag == "Teleporter")
      {
         movement = new Vector3(0,0,0);
         _GameManager.GetComponent<GameManager>().Teleporter();
      }
    }
}

First i can see you have a good understanding of vector math (at least from what we can see in the code), but there are some things that should be done different in Unity. The most important one is, a continously AddForce should only be used in FixedUpdate which is mentioned on the AddForce page at the very bottom.

The other improvements aren’t really necessary, but they increase the performance :wink:

  • Since you use your GameManager quite often, you should store a direct reference or use a singleton. You might want to do the same for the main camera’s transform, but only if you don’t exchange the maincam during the game.
  • Not really a performance thing, but easier to read: .TransformDirection(Vector3.forward) equals .forward. Same thing for .right and .up.

I’m not sure what’s the purpose of “movement”, but i guess you know where you need it… :wink:

Now to the actual question. To make your player jump, you just need to apply an upward velocity boost. You don’t want to add a force which results in an acceleration. You want to directly increase the y speed. At the AddForce page you can see it has a second parameter: ForceMode. Use ForceMode.VelocityChange. This way the value you provide to AddForce isn’t in newton. It’s a direct velocity change.

You would use Input.GetButtonDown to detect the jump key (which you should configure in the inputmanager). GetButtonDown means it’s only executed once when you press the button down. Here you want to Add the upwards-speed. This is also the place where you would play the jump sound.

To play a sound you need an AudioSource component on your player. There are many different ways to play sounds. The easiest way is to have a seperate AudioSource for each different sound (jump, landing, …). To play a sound you just need to call Play on a particular AudioSource to make it play it’s assigned sound. Feel free to explore the scripting reference for other ways.

Landing sounds are always a bit tricky, depending on when you want them and when not. Usually you would use OnCollisionEnter and check for the ground. You could check the surface normal if it’s pointing upwards. You could also ignore such checks. This would play the sound when ever you hit something. You also might want a velocity check at the collision and only play a sound if the impact is stronger.

Here are some changes i would make:

// [...]
private GameManager _GameManager;
// [...]

void Start()
{
    _GameManager = GameObject.Find("_GameManager").GetComponent<GameManager>();
    // or maybe this, if there is only one GameManager:
    // _GameManager = FindObjectOfType(typeof(GameManager)) as GameManager;
}
// [...]

// And then of course:
_GameManager.Death();

As said, if the player always has the same main-cam:

// [...]
private Transform _MainCamera;
// [...]

// in Start():
_MainCamera = Camera.main.transform;

Finally i recommend to read the scripting reference very carefully (and quite often). There are always some things you might have missed the first time or that you simply don’t needed the first time. If the examples in the scripting reference doesn’t explain the usage good enough (unfortunately some examples are really bad or even wrong…), just type it into google. There are tons of informations out there one nearly every aspect of Unity. Most things can be found either here on UA or on the UnityForums.