- Home /
Problem is not reproducible or outdated
Player Footsteps in stairs Problem
Hi!
I have a problem with my footsteps script when I walk down some stairs ingame. Just watch the video below to see what happens.
Video: https://www.youtube.com/watch?v=fHtyNNV0ELA
Does anyone know why it's like that?
Footstep script:
public var walkSounds : AudioClip[];
public var runSounds : AudioClip[];
public var walkAudioSpeed : float = 0.4;
public var runAudioSpeed : float = 0.2;
private var walkAudioTimer : float = 0.0;
private var runAudioTimer : float = 0.0;
var isWalking : boolean = false;
var isRunning : boolean = false;
public var walkSpeed : float = 8;
public var runSpeed : float = 20;
public var sprintDuration : float = 10.0;
private var sprintTimer : float = 0;
private var stamina : float = 0.0;
var CamAnimationScript : CamAnimation;
//public var staminaTexture : Texture;
private var chCtrl: CharacterController;
private var chMotor: CharacterMotor;
public var flashlightAnimation : GameObject;
function Start()
{
chCtrl = GetComponent(CharacterController);
chMotor = GetComponent(CharacterMotor);
}
function Update()
{
SetSpeed();
if ( chCtrl.isGrounded )
{
PlayFootsteps();
}
else
{
walkAudioTimer = 1000.0;
runAudioTimer = 1000.0;
}
}
function SetSpeed()
{
var speed = walkSpeed;
if ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ))
{
if ( Input.GetKey( "left shift" ) || Input.GetKey( "right shift" ) )
{
// Running
isWalking = false;
isRunning = true;
CamAnimationScript.running = true;
CamAnimationScript.walking = false;
}
else
{
// Walking
isWalking = true;
isRunning = false;
flashlightAnimation.animation.CrossFade ("Flashlight", 0.2);
CamAnimationScript.running = false;
CamAnimationScript.walking = true;
}
}
else
{
// Stopped
isWalking = false;
isRunning = false;
flashlightAnimation.animation.CrossFade ("Flashlight", 0.2);
}
// check the sprint timer
if ( isRunning )
{
sprintTimer += Time.deltaTime;
if ( sprintTimer > sprintDuration )
{
isRunning = false;
isWalking = true;
CamAnimationScript.running = false;
CamAnimationScript.walking = true;
}
else if ( chCtrl.isGrounded )
{
speed = runSpeed;
}
}
else
{
sprintTimer -= Time.deltaTime;
}
sprintTimer = Mathf.Clamp( sprintTimer, 0, sprintDuration );
chMotor.movement.maxForwardSpeed = speed;
stamina = 1.0 - ( sprintTimer / sprintDuration );
}
function PlayFootsteps()
{
// Play Audio
if ( isWalking )
{
if ( walkAudioTimer > walkAudioSpeed )
{
audio.Stop();
audio.clip = walkSounds[ Random.Range( 0, walkSounds.Length ) ];
audio.Play();
walkAudioTimer = 0.0;
flashlightAnimation.animation.CrossFade ("Flashlight", 0.2);
}
}
else if ( isRunning )
{
if ( runAudioTimer > runAudioSpeed )
{
audio.Stop();
audio.clip = runSounds[ Random.Range( 0, runSounds.Length ) ];
audio.Play();
runAudioTimer = 0.0;
flashlightAnimation.animation.CrossFade ("FlashlightSprint", 0.2);
}
}
else
{
audio.Stop();
}
// increment timers
walkAudioTimer += Time.deltaTime;
runAudioTimer += Time.deltaTime;
}
//function OnGUI()
//{
// if ( staminaTexture ) // check there is a texture so it doesn't error if not
// {
// GUI.BeginGroup(new Rect(30, Screen.height - 50, 405 * stamina, 25));
// GUI.DrawTexture(new Rect(0, 0, 405, 25), staminaTexture);
// GUI.depth = 0;
// GUI.EndGroup();
// }
//}
video link is private for me. At a guess, the character controller is returning isGrounded as false, and the character controller is actually sliding down the stairs. This is where its happening :
if ( chCtrl.isGrounded )
{
PlayFootsteps();
}
else ....
I don't have an immediate solution rather than to :
see if changing the Slope Angle variable on the character controller helps detect grounded on stairs (are you using the legacy or the sample assets beta controller?)
do your own isGrounded raycast check : if when the character controller finds the slopeAngle too large; of if the rayhits a specific tagged collider.
http://answers.unity3d.com/questions/596645/limited-sprint.html
No difference when I change the slope angle. I'm using the Standard Assets controller
Answer by hexagonius · Jan 05, 2015 at 05:47 PM
I guess you're using the Move function of the Character Controller. You're slope is very steep. I would say the problem is, that for the downward movement, the default gravity is just not enough to keep you constantly on the floor. You're kind of micro hopping down the stairs. You should check for stairs via code and try either increasing the gravity during that time (I did that), or you adjust the move direction to match the slope of the stairs so you're controller moves parallel. To check if the hopping is the case create your own bool variable, make it public and set it every frame to the isGrounded value of the controller. You should see it flickering in the inspector while moving down.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Toggle map/Camera [JS] 1 Answer
Problem with my car enter/exit script 1 Answer
C# scriping help 0 Answers
supplied parameters doesn't match the rpc declaration 1 Answer