How can I rotate UI elements like a hand of cards?

float alignResult = cardIndex / (cards.Count - 1.0f);
newCard.transform.rotation = Quaternion.Euler(0, 0, Mathf.Lerp(15, -15, alignResult));

This is the script I have so far. It was sort-of working, but ran into an infinite loop and stopped working.

I want to adjust UI elements to give a slight rotation based on their position in the order, but this solution isn’t working.

Updated to this:

newCard.transform.rotation = Quaternion.Euler(0, 0, Mathf.Lerp(15, -15, index / cards.Count));

Closer to the intended behavior, but it doesn’t work for some cases, like if there’s only one element.

Edit: Err, I mean, it works, but I get this error: Assertion failed on expression: ‘CompareApproximately(SqrMagnitude(result, 1.0f)’

I highly recommend not mashing up enormous statements like the above.

The first code example is a bit better, but still, break it up more.

Compute and debug the third term for Mathf.Lerp() first and assign it to a temporary variable and you’ll discover many things.

You may discover you’re dividing integers by integers, which of course yields integers.

If you divide 2 by 5 you will get zero in integer land.

Imagine! You may discover that 1 - 1.0f is zero in the first block of code. Dividing by zero yields nothing but delicious NaN bread, unfortunately without garlic.

You may also discover in the second block of code that you’re dividing by zero, although I suppose if cards.Count is zero, none of this would run, but I am only inferring that.

If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.

Putting lots of code on one line DOES NOT make it any faster. That’s not how compiled code works.

The longer your lines of code are, the harder they will be for you to understand them.

How to break down hairy lines of code:

http://plbm.com/?p=248

Break it up, practice social distancing in your code, one thing per line please.

“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums

“Combining a bunch of stuff into one line always feels satisfying, but it’s always a PITA to debug.” - StarManta on the Unity3D forums