-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
Description
In my new 2D project I am using raycasts to keep the player aligned to the ground, which has randomly generated slopes.
here is an example of how it works:
RaycastHit hit;
//Raycast downward from the sprite's current position
if (Physics.Raycast(transform.position + transform.up * 0.3f, -transform.up, out hit, 100f))
{
//Set the transform.up property to the normal (perpendicular vector) of the surface
transform.up = hit.normal;
/* Adjust the transform's position to be at the point where the raycast hit.
* The SpriteHeightOffset (float) simple offsets the sprite so that the
* position is at the feet of the sprite and not the center.
*/
transform.position = hit.point + transform.up * SpriteHeightOffset;
}This method should also work well in Void Runner to rotate the ship and make it align itself to the tube during turns, I estimate this method will get rid of the bug we've had forever where the ship doesn't rotate with the surface, I also think our MoveRight and MoveLeft functions can be cut down significantly.
Reactions are currently unavailable