- Home /
How to test if a charactor is walking on terrain (to make a walking noise)
Hello, so I have this walking clip that I wanted to add to the charactor motor script to make it play a sound every time I moved. When i did that, it played that sound if I was moving in the air, jumping, or just standing still (as well as walking). What would be the best way to play this sound when I'm just moving on the ground.
Also, I don't want a testfor W,A,S,D becuase it would still play that sound when I'm in the air and holding those buttons down. (Java scipt would be appreciated since I'm still new and trying to get used to unity)
Thanks in advance!
Answer by DoTA_KAMIKADzE · Apr 10, 2015 at 05:25 PM
You can use Linecast or Overlap or Raycast to detect a collision with the objects below the character and detect if it is a ground or not.
Here I've found an example with Raycast for you.
Then simply add boolean parameters or int parameter in your animator to transfer from your one of your state to another.
And finally add code for animating if you are on the ground. Here is an example code for boolean if it was called "Walk":
public class PlayerScript : MonoBehaviour
{
private Animator _anim;
[SerializeField]
private string walkAnimation = "Walk";
private bool grounded = true;
internal bool isWalking
{
get { return _anim.GetBool(walkAnimation); }
set { _anim.SetBool(walkAnimation, value); }
}
private void Awake()
{
_anim = GetComponent<Animator>();
}
private void Update()
{
//blablabla
isWalking = grounded;//walking if on ground
//blablabla
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613192728im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enemy moving sideways 1 Answer
Changing the light color with sliders 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Collision Detection 0 Answers