One of the challenges you'll navigate in Monumental Failure is steering characters who have roll around on giant balls. In this blog post, I'm going to walk through the implementation of such a character with a specific focus on how to correlate the character's speed to the ball's rotation. Let's take a look at the final product first. In Monumental Failure, the characters a typically composed from a single rigidbody and a few primitive colliders to represent their physical form. In the case of the ball riders, they are represented by a capsule for the human, and a sphere for the ball. The animated components, the human and the ball, are rotated as child objects while the parent rigidbody has a frozen rotation. When we animate this character, the result should be as follows: the character faces forward, the ball rolls forward, and the character walks backwards, implying they are rolling the ball forward. Facing the character the right way and getting them to walk backwards are trivial problems already implemented in other levels. It took me a bit of trouble to figure out what was the best method to rotate the ball graphics object (remember it is a child of the top level rigidbody). The best way was simply Transform.Rotate, passing Space.World as the relativeTo parameter. The code is something like: public GameObject sphere; And the result is: Not bad right? Take a closer look though, a little slidey maybe? Hmmmm. Be suspicious of my code above, I'm rotating stuff in FixedUpdate and not using Time.fixedDeltaTime. Something must be amiss! At this point we've simply got lucky that our character's speed was close to the amount we need to rotate by, but it's not accurate! We can do better! What we want to do is exactly correlate the character's speed to the rotation of the ball. If we multiply the character's velocity by the fixed delta time, we will get how far the character has traveled in the last frame. To correlate that distance to an amount of rotation, we simply use the circumference of the sphere to figure out how many degrees we need to rotate per unit moved. For example, if our circle has a circumference of 4 units, and our character has moved 1 unit, we would rotate 90 degrees. Easy! public GameObject sphere; Not bad eh? I hope this bit of behind the scenes tech talk was interesting, or maybe even helpful!
Thanks for reading, Spencer
0 Comments
Your comment will be posted after it is approved.
Leave a Reply. |
Archives
March 2017
Categories |