Skip to content

IDEA: Refactor player movement #29

@StefanoFiumara

Description

@StefanoFiumara

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions