Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Stormy102 · Jul 19, 2014 at 09:48 AM · javascriptaudiofootsteps

Footsteps script not working

I want a footsteps script with stamina. I have already implemented the sound but cannot implement the stamina. If anybody could modify the code or tell me how to do this, I would be very greatful. Here is what I have done so far:

 #pragma strict
 @script RequireComponent( AudioSource )
  
 var walk : AudioClip;
 var run : AudioClip;
 var water : AudioClip;
  
 var walkAudioSpeed : float = 0.4;
 var runAudioSpeed : float = 0.2;
  
 private var walkAudioTimer : float = 0.0;
 private var runAudioTimer : float = 0.0;
  
 @HideInInspector
 var isWalking : boolean = false;
 @HideInInspector
 var isRunning : boolean = false;
  
 var walkSpeed: float = 7; // regular speed
 var runSpeed: float = 20; // run speed
 
 @HideInInspector
 var inWater : boolean = false;
 
 private var chCtrl: CharacterController;
 private var chMotor: CharacterMotor;
 
 //Stamina Vars
 private var maxStamina : float = 100.0;
 private var ranOut : boolean = false;
 var Stamina : float = 100.0;
 var staminaRespawnRate : float = 5.5;
 
 function Start()
 {
     chCtrl = GetComponent(CharacterController);
     chMotor = GetComponent(CharacterMotor);
 }
  
 function Update()
 {
     SetSpeed();
  
     if ( chCtrl.isGrounded )
     {
         PlayFootsteps();
     }
     else
     {
         walkAudioTimer = 0.0;
         runAudioTimer = 0.0;
     }
 }
  
 function SetSpeed()
 {
     //Normal health regeneration
     if(Stamina < 100)
     {
         Stamina += Time.deltaTime * staminaRespawnRate;
     }
     
     //When health reaches 100, set to maxHealth
     if(Stamina > maxStamina)
     {
         Stamina = maxStamina;
     }
     var speed = walkSpeed;
     if (Stamina > 0)
     {
         if ( chCtrl.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift") )
         {
             speed = runSpeed;
         }
      
         chMotor.movement.maxForwardSpeed = speed;
     }
     else if (Stamina <= 0)
     {
         //Stamina = 0;
         ranOut = true;
         speed = walkSpeed;
         chMotor.movement.maxForwardSpeed = speed;
         isWalking = true;
         isRunning = false;
     }
     if (Stamina <= 25)
     {
         ranOut = false;
     }
     
 }
  
 function PlayFootsteps() 
 {
     if ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ) )
     {
        if ( Input.GetKey( "left shift" ) || Input.GetKey( "right shift" ) )
        {
             if (Stamina > 1 && ranOut == false)
             {    
                  // Running
                  isWalking = false;
                  
                  Stamina -= 1;
                  isRunning = true;
             }
             else
             {
                 // Walking
                  isWalking = true;
                  isRunning = false;
             }
         }
        else
        {
          // Walking
          isWalking = true;
          isRunning = false;
        }
     }
     else
     {
        // Stopped
        isWalking = false;
        isRunning = false;
     }
  
     // Play Audio
     if ( isWalking && inWater)
     {
        if ( audio.clip != water )
        {
          audio.Stop();
          audio.clip = water;
        }
  
        //if ( !audio.isPlaying )
        if ( walkAudioTimer > walkAudioSpeed )
        {
          audio.Stop();
          audio.Play();
          walkAudioTimer = 0.0;
        }
     }
     else if ( isWalking )
     {
        if ( audio.clip != walk )
        {
          audio.Stop();
          audio.clip = walk;
        }
  
        //if ( !audio.isPlaying )
        if ( walkAudioTimer > walkAudioSpeed )
        {
          audio.Stop();
          audio.Play();
          walkAudioTimer = 0.0;
        }
     }
     else if ( isRunning && inWater)
     {
        if ( audio.clip != water )
        {
          audio.Stop();
          audio.clip = water;
        }
  
        //if ( !audio.isPlaying )
        if ( runAudioTimer > runAudioSpeed )
        {
          audio.Stop();
          audio.Play();
          runAudioTimer = 0.0;
        }
     }
     else if ( isRunning )
     {
        if ( audio.clip != run )
        {
          audio.Stop();
          audio.clip = run;
        }
  
        //if ( !audio.isPlaying )
        if ( runAudioTimer > runAudioSpeed )
        {
          audio.Stop();
          audio.Play();
          runAudioTimer = 0.0;
        }
     }
     else
     {
        audio.Stop();
     }
  
     // increment timers
     walkAudioTimer += Time.deltaTime;
     runAudioTimer += Time.deltaTime;    
 }

Thanks,

Stormy102

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Stormy102 · Jul 19, 2014 at 10:53 AM 0
Share

Anybody got any ideas?

avatar image Eluate · Jul 19, 2014 at 11:04 AM 0
Share

What's the problem? Put some debug.log's in your code and test where the code gets up to.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by AlucardJay · Jul 19, 2014 at 11:10 AM

I have already added a stamina timer to my script on this answer : http://answers.unity3d.com/questions/596645/limited-sprint.html

Scroll down and use the script after Edit

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I stop footstep sound from playing when I release 'w'? 1 Answer

Footstep Audio Check Collider Hitting Floor 1 Answer

Random Audioclip. No Repeat ?? 1 Answer

My Footstep isn't working 2 Answers

Scale messing with code 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges