Hi, first off, sorry for the slightly ambiguous question title but I wasn’t sure how else to put it. Ok, so I wanted to create a C# script to make a footstep sound effect play when the player is moving. Here’s some of what I had in the script:
using UnityEngine;
using System.Collections;
public class FootStepSound : MonoBehaviour
{
public Vector3 previousposition;
public bool ismoving = false;
IEnumerator AudioDelay()
{
yield return new WaitForSeconds(0.5f);
this.gameObject.audio.Play();
}
void Start()
{
previousposition = transform.position;
}
void Update()
{
if(transform.position != previousposition)
{
StartCoroutine(AudioDelay());
}
print (previousposition.ToString () + transform.position.ToString());
if(this.gameObject.audio.isPlaying == false)
{
if(Input.GetKeyDown(KeyCode.UpArrow))
{
ismoving = true;
}
}
if(Input.GetKeyUp(KeyCode.UpArrow))
{
this.gameObject.audio.Stop();
ismoving = false;
}
if(this.gameObject.audio.isPlaying == false)
{
if(Input.GetKeyDown(KeyCode.DownArrow))
{
ismoving = true;
}
}
if(Input.GetKeyUp(KeyCode.DownArrow))
{
this.gameObject.audio.Stop();
ismoving = false;
}
if(Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.DownArrow))
{
this.gameObject.audio.Stop();
ismoving = false;
}
if(ismoving)
{
}
else
{
previousposition = transform.position;
}
}
}
I expected this to play the sound effect while the player is moving, but instead it plays when the player stops moving, when surely transform.position does equal previousposition? I even added code to print tranform.position and previous position so I could compare them as I tested the game. If anyone can help me out it would be great. Thanks.