Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Light997 · Feb 05, 2016 at 05:51 PM · c#returncalling

Make a function call itself and return nothing until x = true

Hi!

I am trying to make a function that checks whether a position is within certain bounds and if false, rerolls the position and checks again. However, I can't find a way to make the function call itself until that condition is fulfilled. I also want it to return a Vector3, so I haven't tried using IEnumerators yet, but if there is a good solution involving IEnumerators, I would of course use that.

Here is my code example:

     Vector3 RepeatCheck(Vector3 position, Vector3 playerPosition)
     {
         if (position.x >= posXLimit.position.x ||
             position.x <= negXLimit.position.x ||
             position.z >= posZLimit.position.z ||
             position.z <= negZLimit.position.z)
         {
             position = SetPosition(position, playerPosition);
             RepeatCheck(position, playerPosition);
         }
 
         if (Mathf.RoundToInt(position.x) == playerPosition.x &&
             Mathf.RoundToInt(position.z) == playerPosition.z)
         {
             position = SetPosition(position, playerPosition);
             RepeatCheck(position, playerPosition);
         }
         return position;
     }

The lower part is only intended to check whether the object is on top of the player. I try to avoid that because I've experienced glitches involving falling through the terrain when that happened, since the object has a collider on it.

Any help is appreciated!

Thank you in advance!

Comment
Add comment · Show 3
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 NoseKills · Feb 07, 2016 at 12:51 PM 0
Share

Right now the function is calling itself if position is outside the posLimitX & Z. If the function does not call itself, the conditions must be false.

I can see a call to SetPosition but i can't see anything that would 'reroll the position'.

What does SetPosition do?

If you use a loop or a recursive function call like this to 'move' something, you have to realize that no rendering happens until the whole process is complete. You will only see the start and end position on your screen. If this is intentional, you could probably calculate the wanted end position mathematically ins$$anonymous$$d of trying and erring until a satisfying position is found.

If this is not intended (you want position to change over time and see the gradual movement on screen) you have to do the moving in Update or such.

avatar image Light997 NoseKills · Feb 07, 2016 at 01:27 PM 0
Share

@Nose$$anonymous$$ills I solved it myself!

All it involved was replacing the if loops with while loops. But for you and anyone else who might be interested:

I set the position of an object within a certain range of the player, for example 50. However, the objects also have to stay within the bounds of another area, which is why I reroll the position until this condition is satisfied.

There is no active movement involved because the objects haven't even been instantiated yet. I am setting the positions of Vector3s within a List. Later, an object will be instantiated for every Vector3 in the List.

Thanks anyway!

avatar image LazyElephant Light997 · Feb 13, 2016 at 09:10 AM 0
Share

@Light If all you did was replace your if statements with while loops, your program is wasting time. Because you're not using the return value of your recursive RepeatCheck calls, every time you call it recursively will just be a waste of time.

To fix this, see my answer below or disregard if you already removed your recursive calls to RepeatCheck.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LazyElephant · Feb 07, 2016 at 03:58 PM

You're not using the return value of your recursive calls. Changing all your RepeatCheck calls to return RepeatCheck(position, playerPosition); Should solve it.

Alternatively, you can just use a loop

 Vector3 RepeatCheck(Vector3 position, Vector3 playerPosition)
  {
      while( (position.x >= posXLimit.position.x ||
          position.x <= negXLimit.position.x ||
          position.z >= posZLimit.position.z ||
          position.z <= negZLimit.position.z) && 
         (Mathf.RoundToInt(position.x) == playerPosition.x &&
          Mathf.RoundToInt(position.z) == playerPosition.z))
      {
          position = SetPosition(position, playerPosition);
      }
      return position;
  }
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

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

Related Questions

Premature level loading. String loads level, instead of Return key. 2 Answers

My Bool function is returning False but it should be true in my opinion. Csharp C# 0 Answers

Return key triggering a button OnClick? 1 Answer

Same code returning different results 1 Answer

Calling a function from another 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