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 jmparavicini · Oct 01, 2014 at 12:00 AM · soundjavafootstepscrouchsprint

Improved Footsteps system

I have this Code for the footsteps

 #pragma strict
 
 var walkSpeed : float = 7;
 var crouchSpeed : float = 3;
 var sprintSpeed : float = 13;
 static var Crouch = false;
 var isCrouching = false;
 var isWalking = false;
 var isRunning = false;
 var isIdle = true;
 
 static var isRunning2 = false;
 static var isCrouching2 = false;
 static var isWalking2 = true;
 
 var speedTest : float = 0;
 
 var footSteps : AudioClip;
 var footCool : float = 0.6;
 var footTimer : float = 0.0;
 
 private var charMotor : CharacterMotor;
 private var charController : CharacterController;
 private var theTransform : Transform;
 private var charHeight : float;
 
 function Start () 
 {
     charMotor = GetComponent(CharacterMotor);
     theTransform = transform;
     charController = GetComponent(CharacterController);
     charHeight = charController.height;
 }
 
 function Update () 
 {
     audio.volume = 1;
     
     isWalking = false;
     isRunning = false;
     isCrouching = false;
     isIdle = true;
     
     if(isWalking == true)
     {
         isWalking2 = true;
         isCrouching2 = false;
         isRunning2 = false;
     }
     
     if(isRunning == true)
     {
         isCrouching2 = false;
         isWalking2 = false;
         isRunning2 = true;
     }
     
     if(isCrouching == true)
     {
         isCrouching2 = true;
         isWalking2 = false;
         isRunning2 = false;
     }
     var h = charHeight;
     var speed = walkSpeed;
     Crouch = false;
     
     if(charController.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift"))
     {
         speed = sprintSpeed;
         isRunning = true;
         isWalking = false;
         isCrouching = false;
         isIdle = false;
     }
     
     if(Input.GetKey("left alt") || Input.GetKey("right alt"))
     {
         h = charHeight*0.5;
         speed = crouchSpeed;
         isRunning = false;
         isWalking = false;
         isCrouching = true;
         isIdle = false;
     }
     
     if(Input.GetKey(KeyCode.W) && isRunning == false && isCrouching == false)
     {
         isWalking = true;
         isCrouching = false;
         isRunning = false;
         isIdle = false;
     }
 
         
     charMotor.movement.maxForwardSpeed = speed;
     var lastHeight = charController.height;
     charController.height = Mathf.Lerp(charController.height, h, 5*Time.deltaTime);
     theTransform.position.y += (charController.height-lastHeight)/2;
     
     if(isWalking == true && Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
     {
         footCool = 0.6;
         stepAudio();
     }
     
     if(isRunning == true && Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
     {
         footCool = 0.4;
         stepAudio();
     }
     
     if(isCrouching == true && Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
     {
         footCool = 0.75;
         stepAudio();
     }    
     
     if(footTimer > 0)
     {
         footTimer -= Time.deltaTime;
     }
     
     if(footTimer < 0)
     {
         footTimer = 0;
     }
 }
 
 function stepAudio()
 {
     if(footTimer == 0)
     {
         audio.PlayOneShot(footSteps);
         footTimer = footCool;
     }
 }

but the problem is that everyfootstep is exactly the same as the last one like a clock that just does tik tik tik but i want to do it that every second footstep is different so its more real like tik tok tik tok any help is apreciated and maybe other improvements for this script would be awesome too because the footsteps are terrible skullbeats1

Comment
Add comment
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

1 Reply

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

Answer by zeman97 · Oct 01, 2014 at 01:20 AM

You could rewrite your stepAudio function as such:

 var isRightFoot : boolean;
     
 var rightFootSound : AudioClip;
 var leftFootSound : AudioClip;
     
 function stepAudio()
 {
     if(isRightFoot)
     {
         audio.PlayOneShot(rightFootSound);
         isRightFoot = false;
     }
     else
     {
         audio.PlayOneShot(leftFootSound);
         isRightFoot = true;
     }
 }

The function keeps track of whether or not to use the left or right foot AudioClip and then sets the isRightFoot variable to the opposite value at the end to allow for alternating.

Comment
Add comment · Show 3 · 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
avatar image jmparavicini · Oct 01, 2014 at 10:25 AM 0
Share

Thank you gonna try it :D

avatar image jmparavicini · Oct 01, 2014 at 05:47 PM 0
Share

i changed it a bit but thank you

avatar image zeman97 · Oct 02, 2014 at 12:32 AM 0
Share

No problem glad to hear it worked! :)

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

27 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 avatar image avatar image avatar image

Related Questions

Footstep Sound Error toSample > fromSample 1 Answer

When an unity game on android is not focused and tries to call android api to play sound, the game will crash. 0 Answers

Make footstep script 2 Answers

Footsteps Java Script Problems 0 Answers

help with sound 1 Answer


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