• 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
1
Question by LEMONVirus99 · Dec 17, 2012 at 09:00 PM · soundjava

scripting error JS. How can i get my FPS player to have footsteps?

So i went around searching for a code and i came across this, however, its giving me a lot of errors i dont understand (i know nothing about scripting) heres the code. Also can someone tell me if this is a correct code to sound footsteps accurately even when my player begins to run? Thanks.

 #pragma strict
 
 var walk : AudioClip; 
 var run : AudioClip;
 
 var isWalking : boolean = false; 
 var isRunning : boolean = false;
 
 function Update() 
 { 
 GetState(); 
 PlayAudio(); 
 }
 
 function GetState() 
 { 
 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; } }
 
 function PlayAudio() 
 { 
  if ( isWalking ) 
 { 
  if ( audio.clip != walk ) 
 { 
  audio.Stop() ; 
  audio.clip = walk; 
 }
 
    if ( !audio.isPlaying )
    {
      audio.Play();
    }
 }
 else if ( isRunning )
 {
    if ( audio.clip != run )
    {
      audio.Stop();
      audio.clip = run;
    }
 
    if ( !audio.isPlaying )
    {
      audio.Play();
    }
 }
 else
 {
    audio.Stop();
 }
 }
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 fafase · Dec 18, 2012 at 07:56 AM 1
Share

You probably have another script with all the movements for your player. You'd rather do all that there. You would save scripts, function calls and global variables.

In the movement script it would be a max total of 5 lines of code. EDIT: plus variable declaration so ok maybe 7...

avatar image LEMONVirus99 · Dec 18, 2012 at 08:07 AM 0
Share

i havent got a great scripting knowledge so this would be easier for me at this moment in time, but thanks for the advice

1 Reply

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

Answer by clunk47 · Dec 18, 2012 at 12:34 AM

Well here's the script with no errors or warnings. Didn't have time to test actual functionality so just let me know what happens and I'll try to get to more specifics if this doesn't do the trick.

  #pragma strict
     
     var walk : AudioClip; 
     var run : AudioClip;
     var isWalking : boolean = false; 
     var isRunning : boolean = false;
     
     function Update() 
     { 
         GetState(); 
         PlayAudio(); 
     }
     
     function GetState() 
     { 
         if(Input.GetAxis("Horizontal") || Input.GetAxis("Vertical")) 
         {
             if(Input.GetKey("left shift") || Input.GetKey("right shift")) 
             { 
                 isWalking = false; 
                 isRunning = true; 
             } 
             else 
             { 
                 isWalking = true; 
                 isRunning = false; 
             }
         }
         else
         {
             isWalking = false;
             isRunning = false;
         }
     }
     
     function PlayAudio() 
     { 
         if(isWalking) 
         { 
             if (audio.clip != walk) 
             { 
                 audio.Stop() ; 
                 audio.clip = walk; 
             }
             if(!audio.isPlaying)
             {
                 audio.Play();
             }
         }
         else if (isRunning)
         {
             if(audio.clip != run)
             {
                 audio.Stop();
                 audio.clip = run;
             }
             if(!audio.isPlaying)
             {
                 audio.Play();
             }
         }
         else
         {
             audio.Stop();
         }
     }
     
 
Comment
Add comment · Show 11 · 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 clunk47 · Dec 18, 2012 at 12:35 AM 2
Share

You had some lines commented out with // You don't want to have actual commands on the same line because anything on that line will be commented out. If you have a // line for information or comments, be sure to do any actual code on the next line down.

avatar image LEMONVirus99 · Dec 18, 2012 at 07:16 AM 2
Share

it works! Thanks again :)

avatar image clunk47 · Dec 18, 2012 at 07:52 AM 2
Share

Glad I could help :)

avatar image LEMONVirus99 · Dec 18, 2012 at 04:14 PM 2
Share

I have just encountered an error. I already have a script on my player that activates a sound and i can only have one sound playing at a time. so how can i make it so my player has more than one sound playing at once?

avatar image fafase · Dec 18, 2012 at 08:26 PM 3
Share

You might get the sound suddenly cut off if they have some kind of delay or reverb added. An other alternative is to use AudioSource.Play(); this will create an audio source object that gets destroyed on completion of the sound.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Walking sound Script returns with Error: BCE0077 2 Answers

level to level 2 Answers

Data keeps "hanging". TCP client/server 2 Answers

Footsteps? 4 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