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 Zyrac · Aug 19, 2014 at 01:43 PM · c#audiotransform.positionnot working

If transform.position != previousposition not working properly.

Hi, first off, sorry for the slightly ambiguous question title but I wasn't sure how else to put it. Ok, so I wanted to create a C# script to make a footstep sound effect play when the player is moving. Here's some of what I had in the script:

 using UnityEngine;
 using System.Collections;
 
 public class FootStepSound : MonoBehaviour
 {
     public Vector3 previousposition;
     public bool ismoving = false;
 
     IEnumerator AudioDelay()
     {
         yield return new WaitForSeconds(0.5f);
         this.gameObject.audio.Play();
     }
 
     void Start()
     {
         previousposition = transform.position;
     }
 
     void Update()
     {
         if(transform.position != previousposition)
         {
             StartCoroutine(AudioDelay());
         }
 
         print (previousposition.ToString () + transform.position.ToString());
         if(this.gameObject.audio.isPlaying == false)
         {
             if(Input.GetKeyDown(KeyCode.UpArrow))
             {
                 ismoving = true;
             }
         }
         if(Input.GetKeyUp(KeyCode.UpArrow))
         {
             this.gameObject.audio.Stop();
             ismoving = false;
         }
         if(this.gameObject.audio.isPlaying == false)
         {
             if(Input.GetKeyDown(KeyCode.DownArrow))
             {
                 ismoving = true;
             }
         }
         if(Input.GetKeyUp(KeyCode.DownArrow))
         {
             this.gameObject.audio.Stop();
             ismoving = false;
         }
         if(Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.DownArrow))
         {
             this.gameObject.audio.Stop();
             ismoving = false;
         }
         if(ismoving)
         {
         }
         else
         {
             previousposition = transform.position;
         }
     }
 }

I expected this to play the sound effect while the player is moving, but instead it plays when the player stops moving, when surely transform.position does equal previousposition? I even added code to print tranform.position and previous position so I could compare them as I tested the game. If anyone can help me out it would be great. Thanks.

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 smoggach · Aug 19, 2014 at 02:25 PM 1
Share

I don't think it's a good idea to have player input governed by audio output. Have you tried it the other way around?

avatar image Zyrac · Aug 19, 2014 at 02:45 PM 0
Share

$$anonymous$$y intention was to have the audio play when the player is moving, not for the player to move when the audio is playing. I've now deleted the if(this.gameobject.audio.isPlaying == false) statements as they were left over from when I tried to create the audio script in a different way, but it doesn't actually make any difference to how it works.

2 Replies

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

Answer by jokim · Aug 19, 2014 at 02:43 PM

Like smoggach said, Player input should not depend on audio output. The audio should be the one reacting to the player input.

Also, there's quite a bit of duplicate code, which isn't a big deal right now, but it quickly adds up if you're not careful.

I'd suggest splitting your audio code from your player inputs.

Modifying your code, I'd try :

 using UnityEngine;
 using System.Collections;
  
 public class FootStepSound : MonoBehaviour
 {
     public Vector3 previousposition;
     public bool ismoving = false;
     
     //Careful here, your code is still getting called every frame, I'd suggest adding a quick check
     bool soundQueued = false;
     IEnumerator AudioDelay()
     {
         if (!soundQueued)
         {
             soundQueued = true;
             yield return new WaitForSeconds(0.5f);
             this.gameObject.audio.Play();
             soundQueued = false;
         }
     }
     
     void Start()
     {
         previousposition = transform.position;
     }
     
     void Update()
     {
         //Deal with the input first.
         if(Input.GetKeyDown(KeyCode.UpArrow))
         {
             ismoving = true;
         }
         
         if(Input.GetKeyUp(KeyCode.UpArrow))
         {
             ismoving = false;
         }
         
         if(Input.GetKeyDown(KeyCode.DownArrow))
         {
             ismoving = true;
         }
 
         if(Input.GetKeyUp(KeyCode.DownArrow))
         {
             ismoving = false;
         }
         
         if(Input.GetKey(KeyCode.UpArrow) && Input.GetKey(KeyCode.DownArrow))
         {
             ismoving = false;
         }
         
         //Then apply logic
         if(ismoving)
         {
             StartCoroutine(AudioDelay());
         }
         else
         {
             this.gameObject.audio.Stop();
             previousposition = transform.position;
         }
     }
 }
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 Zyrac · Aug 19, 2014 at 03:03 PM 0
Share

Thanks very much, I think that's fixed it. :)

avatar image jokim · Aug 19, 2014 at 03:05 PM 0
Share

I just realized there's no need in keeping the Previous position value. tho you could be using it elsewhere.

avatar image
0

Answer by HexadimensionalerAlp · Aug 19, 2014 at 02:40 PM

Because of this I guess :

 if(this.gameObject.audio.isPlaying == false)
         {
             if(Input.GetKeyDown(KeyCode.UpArrow))
             {
                 ismoving = true;
             }
         }

If I got it right ismoving equals true when you start to walk, but only if the sound is not playing. But as soon as the sound is playing the condition for ismoving is false so ismoving is now false.

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

25 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

Related Questions

Help with C# - destroy at a certain position not working 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

PlayClipAtPoint Qualify with Type Name 2 Answers

Is there a way to make a movement pattern using AnimationCurves? 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