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.
No comments:
Post a Comment