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!
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.
@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!
@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.
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;
}
Your answer
Follow this Question
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