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 /
This question was closed Oct 30, 2013 at 12:05 PM by AlucardJay for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by williampigmeu · Aug 01, 2012 at 08:32 PM · stopstepsfoot

FootSteps Script

Well, I have this script:

var audioStepLength: float = 0.1; var footAudio: AudioClip[];

var controller: CharacterController;

function Awake(){ FootStepsSound(); }

function FootStepsSound(){ while(true){ if (controller.isGrounded && controller.velocity.magnitude > 0.5){ var volume = Mathf.Clamp01(Time.time); controller.audio.PlayOneShot(footAudio[Random.Range(0, footAudio.Length)], volume); yield WaitForSeconds(audioStepLength); } else { yield; } } }

I attach it to a Blank GameObject, and set the Controller to my Player, and attach an AudioSource to my Player, but, when I click Play, the Unity stop working.

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 Mander · Aug 01, 2012 at 08:36 PM 1
Share

ur while is making ur unity crash

avatar image williampigmeu · Aug 01, 2012 at 08:39 PM 0
Share

I know this, can you give me an script? Here is an algorithm:

If Player.Velocity > 0.5 Do: Sound.Play(FootStep); Wait(2s); Repeat If

3 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by williampigmeu · Aug 01, 2012 at 09:04 PM

Oh, sorry, I just got it working, if someone wants it, it needs 2 scripts made by me:

RunSneak (attach to Player):

/*** * RunSneak * Make character run and sneak * * Author: WilliamPigmeu */

// Interacting with BobbingView for a realistic head-bob

static var bobbingSpeed; static var bobbingAmount; static var playerStamina = 250;

static var audioStepLength: float = 0.5; static var stepSound = 0;

var walkSpeed: float = 7; // regular speed var sneakSpeed: float = 3; // sneaking speed var runSpeed: float = 20; // running speed

var walkStep: float = 0.5; var sneakStep: float = 1; var runStep: float = 0.45;

private var isRunning: boolean = false;

private var chMotor: CharacterMotor; private var tr: Transform; private var dist: float; // distance to ground

function Start(){ chMotor = GetComponent(CharacterMotor); tr = transform; var ch:CharacterController = GetComponent(CharacterController); dist = ch.height/2; // calculate distance to ground }

function Update(){ if(chMotor.grounded){ bobbingSpeed = 0.20; bobbingAmount = 0.3; } else { bobbingSpeed = 0; bobbingAmount = 0; }

 var vScale = 1.0;
 var speed = walkSpeed;
 audioStepLength = walkStep;
 stepSound = 2;
 

if (!isRunning){ if (playerStamina < 250){ playerStamina = playerStamina+2; } } else if (isRunning){ if (playerStamina >= 0){ playerStamina = playerStamina-3; } }

isRunning = false;

 if (Input.GetKey("left shift") && chMotor.grounded){ // press LShift to run

if (playerStamina >= 3){ isRunning = true;

audioStepLength = runStep; stepSound = 0;

speed = runSpeed; bobbingSpeed = 0.30; bobbingAmount = 0.3; } else { isRunning = false; } }

 if (Input.GetKey("c")){ // press C to sneak
     speed = sneakSpeed;
     bobbingSpeed = 0.15;
     bobbingAmount = 0.1;
     
     audioStepLength = sneakStep;
     stepSound = 1;
 }
 
 chMotor.movement.maxForwardSpeed = speed; // set max speed
 var ultScale = tr.localScale.y; // crouch/stand up smoothly 
 tr.localScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime);
 tr.position.y += dist * (tr.localScale.y-ultScale); // fix vertical position

}

FootSteps (attach to anything and set controller to Player):

var footAudio: AudioClip[]; var controller: CharacterController;

private var RunSneak: RunSneak = null;

function Awake(){ FootStepsSound(); }

function FootStepsSound(){ while(true){ if (controller.isGrounded && controller.velocity.magnitude > 3){ var volume = Mathf.Clamp01(Time.time); controller.audio.PlayOneShot(footAudio[RunSneak.stepSound], volume); yield WaitForSeconds(RunSneak.audioStepLength); } else { yield; } } }

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

Answer by DNP · Aug 01, 2012 at 08:40 PM

If the while is making it crash, add this right after while

yield WaitForSeconds (1);

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

Answer by soutroll · Oct 29, 2013 at 06:13 PM

 #pragma strict
 @script RequireComponent( AudioSource )
  
 var walk : AudioClip;
 var run : AudioClip;
  
 var walkAudioSpeed : float = 0.4;
 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;
  
 var walkSpeed: float = 7; // regular speed
 var runSpeed: float = 20; // run speed
  
 private var chCtrl: CharacterController;
 private var chMotor: CharacterMotor;
  
 function Start()
 {
     chCtrl = GetComponent(CharacterController);
     chMotor = GetComponent(CharacterMotor);
 }
  
 function Update()
 {
     SetSpeed();
  
     if ( chCtrl.isGrounded )
     {
         PlayFootsteps();
     }
     else
     {
         walkAudioTimer = 0.0;
         runAudioTimer = 0.0;
     }
 }
  
 function SetSpeed()
 {
     var speed = walkSpeed;
  
     if ( chCtrl.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift") )
     {
         speed = runSpeed;
     }
  
     chMotor.movement.maxForwardSpeed = speed;
 }
  
 function PlayFootsteps() 
 {
     if ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ) )
     {
        if ( Input.GetKey( "left shift" ) || Input.GetKey( "right shift" ) )
        {
          // Running
          isWalking = false;
          isRunning = true;
        }
        else
        {
          // Walking
          isWalking = true;
          isRunning = false;
        }
     }
     else
     {
        // Stopped
        isWalking = false;
        isRunning = false;
     }
  
     // Play Audio
     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 )
     {
        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;    
 }
Comment
Add comment · Show 1 · 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 AlucardJay · Oct 30, 2013 at 07:04 AM 1
Share

thanks for posting my script without giving any credit or any reference to where you got it from .....

http://answers.unity3d.com/questions/373508/footsteps-script-for-running-and-walking.html

Follow this Question

Answers Answers and Comments

11 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

Related Questions

A node in a childnode? 1 Answer

Scripts not on instantiated GameObjects? 1 Answer

animating character 1 Answer

iTween(JS version) could not be used in Unity iPhone? 3 Answers

Very simple inventory script... 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