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 crusherxman · May 02, 2013 at 09:36 PM · soundspeedlengthsprint

Decreasing footstep length when key pressed

Hi everyone, I've being working on a horror game and I've found a "Footstep" script witch creates sounds when I'm walking (W,D,S,A Keys) and I've managed to create a Sprint function when pressing the Shift button but there's one thing that doesn't really match together: When I sprint with my Footstep Script, my footstep length remains the same. What I'm saying is when I press the Shift key, I want my sound length to be decreased witch means, increasing my speed of the footstep sound.

So here's my script of the Footstep function:

 var audioStepLength = 0.3;
 var footAudio : AudioClip[];
 
 function OnTriggerEnter (other : Collider) {
     var chao : System.Net.Sockets.SendPacketsElement = other.GetComponent(System.Net.Sockets.SendPacketsElement);
 
     if(chao){
         footAudio = chao.footsteps;
    }
 }
 
 function PlayStepSounds () {
     var controller : CharacterController = GetComponent(CharacterController);
 
     while (true) {
         if (controller.isGrounded && controller.velocity.magnitude > 0.3) {
             var volume = Mathf.Clamp01(Time.time);
             audio.PlayOneShot(footAudio[Random.Range(0, footAudio.Length)], volume);
             yield WaitForSeconds(audioStepLength);
             
    
         } else {
            yield;
         }
    }
 }
 
 function Awake(){
     PlayStepSounds();
 } 

If you guys have a great way to solve my question, please post them below! I would appreciate it :)

Thanks!

~crusherxman

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
0
Best Answer

Answer by dorpeleg · May 02, 2013 at 11:43 PM

You will need to change the audioStepLength from your sprint script.

To start of, you will need a reference to your footsteps script.

You do that by using GetComponent link

Then you need to change audioStepLength to a lower number when running and back to 0.3 when walking.

Comment
Add comment · Show 14 · 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 crusherxman · May 03, 2013 at 08:38 PM 0
Share

I've tried it but my footsteps length just wont work correctly (Duplicates footstep's sound)

$$anonymous$$aybe could I show you my script? (Default, not edited)

avatar image dorpeleg · May 04, 2013 at 11:21 AM 0
Share

Sure, if you show your script it will be easier to help.

avatar image crusherxman · May 04, 2013 at 01:48 PM 0
Share

There you go:

 var walkSpeed: float = 7; // regular speed
 var crchSpeed: float = 3; // crouching speed
 var runSpeed: float = 20; // run speed
 
 private var ch$$anonymous$$otor: Character$$anonymous$$otor;
 private var ch: CharacterController;
 private var tr: Transform;
 private var height: float; // initial height
 
 function Start(){
     ch$$anonymous$$otor = GetComponent(Character$$anonymous$$otor);
     tr = transform;
     ch = GetComponent(CharacterController);
     height = ch.height;
 }
  
 function Update(){
  
     var h = height;
     var speed = walkSpeed;
     
     if (ch.isGrounded && Input.Get$$anonymous$$ey("left shift") || Input.Get$$anonymous$$ey("right shift")){
         speed = runSpeed;
     }
     if (Input.Get$$anonymous$$ey("left ctrl")){ // press C to crouch
         h = 0.5 * height;
         speed = crchSpeed; // slow down when crouching
     }
    ch$$anonymous$$otor.movement.maxForwardSpeed = speed; // set max speed
     var lastHeight = ch.height; // crouch/stand up smoothly 
     ch.height = $$anonymous$$athf.Lerp(ch.height, h, 5*Time.deltaTime);
     tr.position.y += (ch.height-lastHeight)/2; // fix vertical position
 }
avatar image crusherxman · May 04, 2013 at 01:49 PM 0
Share

Oh and I've also added a "Crouch" function, just to re$$anonymous$$d you.

avatar image dorpeleg · May 04, 2013 at 01:51 PM 0
Share

Are both scripts sit on the same gameobject?

Show more comments

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

13 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

Related Questions

Array exists, but won't give length??? 1 Answer

How to use option setting Vibrate and Mute 1 Answer

Energy Pickup 1 Answer

Explosion Jumpscare Script ? 1 Answer

Detect if out of trigger 2 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