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
1
Question by Prosto_Lyubo · Aug 24, 2013 at 11:55 PM · invokerecursion

Simple derivative by invoking recurrently - don't work

Hi! I'm animating pistons in my walker model. I want them to play two kinds of sound depending if the distance is shortening or getting bigger. So here is what i wrote:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerSoundsHandler  : MonoBehaviour {
     public Transform Shaft;
     public Transform Cylinder;
     
     private float[] dist = new float[3];
     private int InvokedNo;
 
     void Start () {
         InvokedNo=0;
         SetDist();
         Invoke("SetDist",0.1f);
         Invoke("SetDist",0.2f);
     }
     
     void Update () {
 
         if (InvokedNo==2){
         Debug.DrawLine(new Vector3(-0.5f,0f,0f),new Vector3(-0.5f,dist[0],0f));
         Debug.DrawLine(new Vector3(-0.5f,0f,0.1f),new Vector3(-0.5f,dist[1],0.1f));
         Debug.DrawLine(new Vector3(-0.5f,0f,0.2f),new Vector3(-0.5f,dist[2],0.2f));
         }
     }
 
     void SetDist(){
         Debug.Log (InvokedNo);
         dist[InvokedNo] = Vector3.Distance(Shaft.position,Cylinder.position);
         Debug.Log (dist[0]+"/"+dist[1]+"/"+dist[2]);
         if (InvokedNo==2){
             if ((dist[0]<dist[1] && dist[2]<dist[1])||(dist[0]>dist[1] && dist[2]>dist[1]))
                 audio.Play ();
             InvokedNo=-1;
             }
         InvokedNo=InvokedNo+1;    
         Invoke("SetDist",0.3f);
     }
     
 }

So my question is: what is wrong with that code? I'm trying to figure it out for like 3 hours. The issue is, that even if something is moving with const. speed dist[2] is always smaller or bigger than the others, except first three times function is invoked (then everything works fine). I've tested it both with my model and on simle spheres (one static, second falling down) - so it is not my model problem. Maybe I missunderstood how to work with Invoke? Thanks for help.

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 meat5000 ♦ · Aug 25, 2013 at 12:02 AM 0
Share

http://docs.unity3d.com/Documentation/Components/class-Audio$$anonymous$$anager.html

avatar image Prosto_Lyubo · Aug 25, 2013 at 12:32 AM 0
Share

Why are You posting audio documentation? I don't have problem with playing Audio. I have problem with checking if a certain value started increasing and stopped decreasing or vice-versa

1 Reply

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

Answer by Bunny83 · Aug 25, 2013 at 02:22 AM

I have to say that is really difficult to understand what you actually want to do and what's your exact problem. You said you want to play two different sounds but you only have one audio source and it seems you don't switch the audioclip.

Next thing is, why are you executing SetDist 3 times with 0.1 sec delay? This would "schedule" SetDist 3 times and each time one is executed it would reschedule itself. Why don't use simply call SetDist once and have it schedule itself every 0.1 seconds since that's effectively what you're doing now but way more complicated.

In such a case InvokeRepeating or a coroutine would make more sense i guess.

Next thing is your "InvokedNo" starts at 0 and is increased each time SetDist runs until it reaches 2 where it would stay for the rest of the game.

That means dist[0] and dist[1] are only set once when InvokedNo is 0 and 1 respectively. Once InvokedNo is 2 you only set dist[2] each call of SetDist.

I actually don't get why you store 3 values. To calculate the delta you only need the old value and the new value.

Something like this should be enough:

 using UnityEngine;
 using System.Collections;
  
 public class PlayerSoundsHandler : MonoBehaviour
 {
     public Transform Shaft;
     public Transform Cylinder;
     private float oldDist = 0;

     float CalculateDist()
     {
         return Vector3.Distance(Shaft.position, Cylinder.position);
     }
     
     void Start ()
     {
         oldDist = CalculateDist();
         InvokeRepeating("CheckDist", 0.1f, 0.1f);
     }
     
     void CheckDist()
     {
         float dist = CalculateDist();
         float delta = dist - oldDist;
         oldDist = dist;
         if (delta > 0f)
         {
             // extending
         }
         else if(delta < 0f)
         {
             // retracting
         }
         else
         {
             // doesn't move
         }
     }
 }

edit
ps: if you only want to know when the delta changes you just need to take the delta from the delta ;) So only another variable that holds the current "direction" and just check if the direction has changed:

     // [...]
     private float oldDir = 0f;
     
     // [...]
     float dir = Mathf.Sign(delta);
     if (dir != oldDir)
     {
         if (delta > 0f)
         // [...]
     }
     oldDir = dir;
     //[...]

In this case it makes more sense to call CheckDist simply in Update and don't use Invoke at all.

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 Prosto_Lyubo · Aug 26, 2013 at 08:27 AM 0
Share

thanks, that is a lot simpler than my solution ; )

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

17 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

Related Questions

Intermittent Stack Overflow Exception 1 Answer

Serialization depth limit 7 exceeded with linked-list-like data structure 3 Answers

invoke keyboard on android? 1 Answer

IsInvoking 0 Answers

Delay If statement in Update c# 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