Scary Wizard Games
  • MONUMENTAL FAILURE
  • DEVELOPMENT LOG
  • ABOUT US
  • CONTACT
  • PRESS KIT

LET'S GET THE BALL ROLLING

7/10/2016

0 Comments

 
Picture
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.
Picture
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.
Picture
Composition of a character.
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;

protected override void FixedUpdate()
{
   sphere.transform.Rotate(myRigidBody.velocity.z, 0, -myRigidBody.velocity.x, Space.World);
}
And the result is:
Picture
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!
Picture
I'll illustrate that point with a character bigger than a pyramid!
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;
    public RigidBody myRigidBody;
    float rotationalSpeed; //In degress per unit

    void Start()
    {
        float diameter = sphere.transform.lossyScale.x;
        float circumfrence = Mathf.PI * diameter;
        rotationalSpeed = 360 / circumfrence;
    }

    protected override void FixedUpdate()
    {
 sphere.transform.Rotate(Time.fixedDeltaTime * rotationalSpeed * myRigidBody.velocity.z, 0, Time.fixedDeltaTime * rotationalSpeed * -myRigidBody.velocity.x , Space.World);
     }

Picture
Voila!
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
    February 2017
    January 2017
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016

    Categories

    All

    RSS Feed

Powered by Create your own unique website with customizable templates.
  • MONUMENTAL FAILURE
  • DEVELOPMENT LOG
  • ABOUT US
  • CONTACT
  • PRESS KIT