- Home /
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.
I don't think it's a good idea to have player input governed by audio output. Have you tried it the other way around?
$$anonymous$$y intention was to have the audio play when the player is moving, not for the player to move when the audio is playing. I've now deleted the if(this.gameobject.audio.isPlaying == false) statements as they were left over from when I tried to create the audio script in a different way, but it doesn't actually make any difference to how it works.
Answer by jokim · Aug 19, 2014 at 02:43 PM
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;
}
}
}
I just realized there's no need in keeping the Previous position value. tho you could be using it elsewhere.
Answer by HexadimensionalerAlp · Aug 19, 2014 at 02:40 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613160950im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help with C# - destroy at a certain position not working 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
PlayClipAtPoint Qualify with Type Name 2 Answers
Is there a way to make a movement pattern using AnimationCurves? 0 Answers