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 Peanut97 · Nov 23, 2013 at 04:38 PM · keycodefootstepsmodify

Can anyone help me modify this simple script?

Hello Everyone,

At the current moment, I am working on making footstep audio. The script I have basically says to play audio when the "W" key is held down, and stop when it is let go. This script works perfectly, but the only problem is if you walk sideways or backwards, the audio doesn't play... Basically, I'm wondering if anybody can help me modify this script so that it has multiple keycodes for "W" "A" "S" and "D." If anybody could help me out with this, I wouls REALLY appreciate it! Thank you so much for your time!

 #pragma strict
  
 var AudioFile : AudioClip;
  
 function Update() 
  
 {
     if (Input.GetKeyDown (KeyCode.W))
     {
        audio.clip = AudioFile;
        audio.Play();
     }
     else if (Input.GetKeyUp (KeyCode.W))
     {
        audio.Stop();
     }
 }
 
 if (Input.GetKeyDown (KeyCode.W))
 {
     audio.clip = AudioFile;
     audio.Play();
  
 }
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

5 Replies

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

Answer by TheLivingTree · Nov 23, 2013 at 05:29 PM

If you just want to extra have key presses try something like this:

     #pragma strict
      
     var AudioFile : AudioClip;
      
     function Update() 
      
     {
         if (Input.GetKeyDown (KeyCode.W) || Input.GetKeyDown (KeyCode.A) || Input.GetKeyDown (KeyCode.S) || Input.GetKeyDown (KeyCode.D))
         {
            audio.clip = AudioFile;
            audio.Play();
         }
         else if (Input.GetKeyUp (KeyCode.W) || Input.GetKeyUp (KeyCode.A) || Input.GetKeyUp (KeyCode.S) || Input.GetKeyUp (KeyCode.D))
         {
            audio.Stop();
         }
     }
 
 
 //Why are you repeating this step? It isn't necessary
     //if (Input.GetKeyDown (KeyCode.W))
    // {
     //    audio.clip = AudioFile;
      //   audio.Play();
      
    // }

The '||' symbol is considered 'or' in logic. So the code is saying in english: if the w or a or d or s key is pressed execute this code. However, rather than use Input.GetKey I would suggest [Input.GetButton][1]. This allows for more flexibility because a menu can be made to change the input mid game. I would highly suggest looking into this, as it will save much time if used early on. [1]: http://docs.unity3d.com/Documentation/ScriptReference/Input.GetButton.html

Comment
Add comment · Show 2 · 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 Peanut97 · Nov 23, 2013 at 05:52 PM 0
Share

Thank you all so much for your help!

avatar image TheLivingTree · Nov 23, 2013 at 05:54 PM 1
Share

$$anonymous$$y pleasure! Thank you for accepting my answer :D

avatar image
1

Answer by drak8888 · Nov 23, 2013 at 09:13 PM

Here you go :

 #pragma strict
      
 var AudioFile : AudioClip;
      
 function Update()
 {
     if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
     {
         audio.clip = AudioFile;
         audio.Play();
     }
     else if (Input.GetAxis("Horizontal") == 0 || Input.GetAxis("Vertical") == 0)
     {
         audio.Stop();
     }
 }
      
 if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
     {
     audio.clip = AudioFile;
     audio.Play();
      
 }

This here script will use the Axis instead of the normal button down. An axis is an Input set in your game when you go in Edit > Project Settings > Input.

By default, the axis are set with WASD and the arrow keys, so this should work perfectly.

Also for the sake of understanding, an Axis either gives 1 when positive (W Key) or -1 when negative (S Key), which is why I only check if the Axis is at 0 or not. When at 0, its not pressed and when its not at 0, its pressed. Simple as that.

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
1

Answer by clunk47 · Nov 23, 2013 at 10:04 PM

The simplest way, use a footstep sound with a second or two of silence at the end of the clip (however much time you want between footsteps), and do something like:

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour 
 {
     void Update()
     {
         if(!audio.isPlaying && (Input.GetAxis ("Horizontal") != 0 || Input.GetAxis("Vertical") != 0))
             audio.Play ();
     }
 }
 
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 Patrykgazing · Nov 23, 2013 at 05:30 PM

Only way i can help you, is that you can always copy input codes 4 times and only change the KeyCode so it would be like:

 #pragma strict
  
 var AudioFile : AudioClip;
  
 function Update() 
  
 {
     if (Input.GetKeyDown (KeyCode.W))
     {
        audio.clip = AudioFile;
        audio.Play();
     }
     else if (Input.GetKeyUp (KeyCode.W))
     {
        audio.Stop();
     }
     else if (Input.GetKeyDown (KeyCode.A))
     {
        audio.clip = AudioFile;
        audio.Play();
     }
     else if (Input.GetKeyUp (KeyCode.A))
     {
        audio.Stop();
     }
     else if (Input.GetKeyDown (KeyCode.S))
     {
        audio.clip = AudioFile;
        audio.Play();
     }
     else if (Input.GetKeyUp (KeyCode.S))
     {
        audio.Stop();
     }
     else if (Input.GetKeyDown (KeyCode.D))
     {
        audio.clip = AudioFile;
        audio.Play();
     }
     else if (Input.GetKeyUp (KeyCode.D))
     {
        audio.Stop();
     }
 }

It's just my basic solve of this.

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 Commander Quackers · Nov 23, 2013 at 05:30 PM

I use this. http://bando.bplaced.net/scripts/Foot%20Steps.js

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

21 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

Related Questions

Multiple Cars not working 1 Answer

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

Simple footsteps? 1 Answer

Player Footsteps in stairs Problem 1 Answer

Help with Simple Footsteps? 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