- Home /
Problem with Footstep System
Hey! I am working on a footstep system with checks the ground via raycast to see on what kind of material the player is standing on and therefore creating the according footsteps. The problem is with the timer, because the "left foot" and "right foot" lines are logged very rapidly (and therefore creating a very weird sound) and in general when looking at the timer, it does not look like it is changing at all. Anybody know why this would be?
Here is a minimised version of my script I am using (it's long, but not very advanced):
public bool isCreeping = false;
public bool rightStep;
public bool leftStep;
public float stepTimer = 0.0f;
[HideInInspector] public float checkRange = 15.0f;
public float stepTime = 5;
public Transform rightFoot;
public Transform leftFoot;
//Checks if the player is in air
public bool grounded = false;
void Update ()
{
//If the player is moving
if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
{
//If the player is not creeping
if(isCreeping == false)
{
//Make foot steps
CheckGround();
}
}
}
public void CheckGround ()
{
RaycastHit hit;
Debug.DrawRay(transform.position, Vector3.down , Color.red);
//Check for colliders below the player
if(Physics.Raycast(transform.position, Vector3.down, out hit ))
{
Vector3 LeftFootPos = leftFoot.position;
Vector3 RightFootPos = rightFoot.position;
string floortag = hit.collider.tag;
//Play sound for the according floor (in this case Terrainisch things like grass or mud)
if(floortag == "mudFloor")
{
//If the timer is below "stepTime"
if(stepTimer < stepTime)
{
//Add to it over time
stepTimer += 1 * Time.deltaTime;
//If it is over or equal to stepTime...
if(stepTimer >= stepTime)
Debug.Log ("Produce Step Sound");
{
//..and the player is on the left foot
if(leftStep == true)
{
//reset the timer
stepTimer = 0.0f;
//play the sound at the left foot position
NetworkedSounds.Instance.Client_GrassFootStep(LeftFootPos);
//the player is gonna walk on the right foot next, so disable left foot...
leftStep = false;
//and enable right foot
rightStep = true;
Debug.Log ("Left Step");
}
if(rightStep == true)
{
//same thing with the right foot
stepTimer = 0.0f;
NetworkedSounds.Instance.Client_GrassFootStep(RightFootPos);
rightStep = false;
leftStep = true;
Debug.Log("Right Step");
}
}
}
}
}
if(grounded == false)
{
//reset the timer if the player happense to be in the air
stepTimer = stepTime;
}
}
That looks pretty interesting! But is unfortunately also not what I'm looking for at the moment.
Answer by tanoshimi · Jan 02, 2014 at 04:49 PM
You intialise grounded as false, and never assign it any other value:
public bool grounded = false;
Therefore this code block runs on every iteration of the Update() loop:
if(grounded == false)
{
//reset the timer if the player happense to be in the air
stepTimer = stepTime;
}
Therefore your timer never changes.
Well this is a snippit of the code and grounded does change (but this has to do with other code that I did not find relevant to include). Sorry for causing some confusion there.
Well it's going to be hard to debug your code if you only paste some of it...