- Home /
Duplicate Question
Collider With a FootStep sound.
Hi to every one I need to create a footsteps sound when character moving, and make it different on different surface. Can I use the Collider.OnTriggerStay(Collider) be used to create a footsteps sound with a hit.transform.collider.tag == “tag”? Before of this I tried to use ControllerColliderHit.collider but it didn’t helped Also I use non standard FPS controller. My FPS controller
public class rigBodyControl : MonoBehaviour { public float speed = 10.0f; public float gravity = 10.0f; public float maxVelocityChange = 10.0f;
private bool grounded = false;
void Awake()
{
rigidbody.freezeRotation = true;
rigidbody.useGravity = false;
}
void FixedUpdate()
{
GetComponent<MainControlAnimation>();
if (grounded)
{
// Calculate how fast we should be moving
Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
targetVelocity = transform.TransformDirection(targetVelocity);
targetVelocity *= speed;
// Apply a force that attempts to reach our target velocity
Vector3 velocity = rigidbody.velocity;
Vector3 velocityChange = (targetVelocity - velocity);
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0;
rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
}
// We apply gravity manually for more tuning control
rigidbody.AddForce(new Vector3(0, -gravity * rigidbody.mass, 0));
grounded = false;
}
void OnCollisionStay()
{
grounded = true;
}
}
Trying to use ControllerColliderHit.collider but in this code but its "Lenght" part in audio.clip = concreteSound[Random.Range(0, concreteSound.Lenght)];has no does not contain a definition.
public CharacterController controller;
public AudioClip concreteSound;
private bool step = true;
float audioStepLengthWalk = 0.45f;
// Use this for initialization
void Start () {
controller = GetComponent<CharacterController>();
}
void OnControllerColliderHit (ControllerColliderHit hit)
{
//Debug.Log("Controller hit the ground");
if (controller.isGrounded && controller.velocity.magnitude < 7 && controller.velocity.magnitude > 5 && hit.gameObject.tag == "Untagged" && step == true )
{
WalkOnConcrete();
}
}
IEnumerator WaitForFootSteps(float stepsLength)
{
step = false;
yield return new WaitForSeconds(stepsLength);
step = true;
}
public void WalkOnConcrete ()
{
audio.clip = concreteSound[Random.Range(0, concreteSound.Lenght)];
audio.volume = 0.1f;
audio.Play();
StartCoroutine(WaitForFootSteps(audioStepLengthWalk));
}
}
Anybody have advise what I miss?
There are so many tuts/vids/duplicate questions on this topic already. Also, its length not Lenght
Is it not better to do a ray cast on the ground and check its type?
Why not create a method in your script that deter$$anonymous$$es which collider/surface it's touching and then plays its corresponding sound. Then call that method during your run/walk animation timelines with an event.