Search Unity

Making sidescroller character adhere to the ground?

Discussion in '2D' started by SgtSargeMcFly, Oct 8, 2021.

  1. SgtSargeMcFly

    SgtSargeMcFly

    Joined:
    May 4, 2020
    Posts:
    20
    I'm making a sidescroller that requires the character to "stick" to the ground and move across it without going airborne. Additionally, if the character hits a wall, they should start moving up it instead of simply coming to a halt. The character acts like a buzzsaw, basically.

    I don't know how to go about this. My best idea is to use RaycastHit.normal and use the output to adjust where the character should be moving, but I don't feel confident about that. No need to write any code for me or anything, but if anyone could point me to some variables or properties that may be helpful to me, I'd appreciate that a lot. Thanks so much for reading!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    That actually kinda feels perfect to me... you would need to consider these things:

    - raycast "local down" (from the player's perspective) each frame and pin the player to the ground there

    - also rotate the player to the desired normal orientation

    - for the above two, you get info back from Raycast in the form of a RaycastHit, which has both position and normal.

    - movement would move the player based on their tilting (left / right would morph to up/ down)

    - use layers to ensure you ONLY hit the ground that you want the player to follow.
     
  3. SgtSargeMcFly

    SgtSargeMcFly

    Joined:
    May 4, 2020
    Posts:
    20
    Thanks a bunch for the feedback. I'm glad I was right about the raycast but after toying around with it, I'm still at a bit of a loss. Is there any method you could recommend for adjusting their direction based on their rotation? I'm using rigidbody.velocity to move the character right now and I'm thinking that might have to go, since it doesn't accept a direction as an argument or change based on the rigidbody's rotation.