- Home /
Wrapping negative numbers
Need a little help. I'm trying to wrap numbers but it needs to allow for negative numbers. This is what i got but it keeps clamping instead of wrapping. ie Debug.Log (Wrap(-210, -120, 120)); should = 50
public float Wrap( float Target, float LowerBound, float UpperBound)
{
float range_size = UpperBound - LowerBound + 1;
if (Target < LowerBound)
Target += range_size * ((LowerBound - Target) / range_size + 1);
return LowerBound + (Target - LowerBound) % range_size;
}
Comment
Answer by KurtGokhan · Jan 20, 2015 at 12:01 PM
I think what you trying to do is:
public float Wrap( float Target, float LowerBound, float UpperBound){
float dif= UpperBound - LowerBound;
if(Target > LowerBound)
return LowerBound + (Target - LowerBound) % dif;
Target = LowerBound + (LowerBound - Target);
float tmpVal = LowerBound + (Target - LowerBound) % dif;
return UpperBound - tmpVal;
}
I think this should work. I can't test it. Can you try?
Your answer
Follow this Question
Related Questions
UV Mapping. How can one point appear in multible places? 1 Answer
TextMesh Pro, indent line that is wrapped. 1 Answer
Help With UV Mapping 1 Answer
Breaking text line in GUI.Box 1 Answer
'Wrapped world' effect with 2 cameras? 2 Answers