Tuesday, August 4, 2015

Rotation of the flame sprite

In my previous blog, I hinted that we wanted to rotate the flame, the idea is that when falling we want the flames above the Shuffa ball, with an upwards motion the flames below and when on the ground the flames to the left of the ball.

Shuffa ball with rotated flame

To do this we need to know when the ball is in contact with the ground, and if airborne whether the trajectory is upwards or downwards.

To see if the ball is in contact with the ground we will use RayCasting, we will cast an invisible line down a certain distance from the centre of the ball; if it collides with a piece of floor then we return true saying it is grounded.

With RayCasting we need to use layers, these layers separate the different object types. If we didn’t then the RayCast will always return true as it casts from the centre of the ball down to outside the circle collider, thus hitting the collider and returning true.
We will assign all of the floor sprites to the Floor layer, then when RayCasting we tell it to only search the Floor layer.

To set up the Floor layer click Layers->Edit Layers, then add the Floor layer to one of the User Layers.
adding the Floor layer
Next assign the floor tile to the Floor Layer.
assign the floor sprite to the Floor layer
To work out whether the ball is falling or on an upward trajectory we compare the current transform position with the previous frame. Then adjust the angle accordingly. When adjusting the angle we don't want it to change immediately but have a smooth transition, that is where the Slerp function comes in.

Video of the ball flame rotation in action

No comments:

Post a Comment