If transform.position != previousposition not working properly.

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.

Like smoggach said, Player input should not depend on audio output. The audio should be the one reacting to the player input.

Also, there’s quite a bit of duplicate code, which isn’t a big deal right now, but it quickly adds up if you’re not careful.

I’d suggest splitting your audio code from your player inputs.

Modifying your code, I’d try :

using UnityEngine;
using System.Collections;
 
public class FootStepSound : MonoBehaviour
{
	public Vector3 previousposition;
	public bool ismoving = false;
	
	//Careful here, your code is still getting called every frame, I'd suggest adding a quick check
	bool soundQueued = false;
	IEnumerator AudioDelay()
	{
		if (!soundQueued)
		{
			soundQueued = true;
			yield return new WaitForSeconds(0.5f);
			this.gameObject.audio.Play();
			soundQueued = false;
		}
	}
	
	void Start()
	{
		previousposition = transform.position;
	}
	
	void Update()
	{
		//Deal with the input first.
		if(Input.GetKeyDown(KeyCode.UpArrow))
		{
			ismoving = true;
		}
		
		if(Input.GetKeyUp(KeyCode.UpArrow))
		{
			ismoving = false;
		}
		
		if(Input.GetKeyDown(KeyCode.DownArrow))
		{
			ismoving = true;
		}

		if(Input.GetKeyUp(KeyCode.DownArrow))
		{
			ismoving = false;
		}
		
		if(Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.DownArrow))
		{
			ismoving = false;
		}
		
		//Then apply logic
		if(ismoving)
		{
			StartCoroutine(AudioDelay());
		}
		else
		{
			this.gameObject.audio.Stop();
			previousposition = transform.position;
		}
	}
}

Because of this I guess :

if(this.gameObject.audio.isPlaying == false)
        {
            if(Input.GetKeyDown(KeyCode.UpArrow))
            {
                ismoving = true;
            }
        }

If I got it right ismoving equals true when you start to walk, but only if the sound is not playing. But as soon as the sound is playing the condition for ismoving is false so ismoving is now false.