Search Unity

Question What's the best approach to do this

Discussion in 'Scripting' started by elcuci, May 21, 2023.

  1. elcuci

    elcuci

    Joined:
    Jun 13, 2021
    Posts:
    17
    I will explain myself as best as possible, let me know if you have any questions. I have a first person controller that uses a character controller, when the move keys are pressed it applies CharacterController.Move() in whichever direction we're moving. The important part is that it only moves horizontally, no vertical movement. Slopes are a problem, but I can just cast a ray and get the normal to determine if I am in one or not. However the same can't be said for stairs. Whenever the character is going down stairs, it starts bouncing for the reason mentioned before, it only moves forward. I have a logic that will make it stick to the stairs, the problem is determining when the player is on them. My logic is the following: if a player is not grounded they're either going down stairs and bouncing, falling or jumping. I only want the player to stick to the ground when going down stairs. It's easy enough to determine if we're jumping, as we have the input. However, falling from a precipice and going down stairs are not so different, in fact I with my tiny brain can only think of two differences: the time the player is in the air and the distance they're falling. My question lies there, which method do you think would be better. A somewhat expensive raycast that checks the distance to the current ground (or stairs) and compares it to a small distance set by the user to check if the distance is great enough to consider the situation falling from a ledge, or if it's not large enough determine we're just walking forward on stairs, or use the other method, which is to set a timer (using a coroutine or whatever) and if the player is in the air for long enough we can say they're falling, and viceversa. This second method works perfectly and I can't think of any disadvantages right now, but it just feels too much like a workaround and my gut tells me it's not going to be flexible. Raycasting seems to make more sense, but it's more expensive and it doesn't even work properly in this situation.

    Also also, any other possible solutions are more than welcome!

    And while we're at it. Should I use the character controller's built in ground check or just make my own? It seems to be a controversial topic.

    Did I explain myself correctly? Hopefully I did. As I said, if there's any questions I'll be happy to respond. And any help is much appreciated!
     
    Last edited: May 21, 2023
  2. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,315
    There is no just one solution to this problem as the most important factor is how your game is constructed. Something might be completely acceptable in game A, while the same thing in game B will be trash solution.
    From my experience it makes way more logical sense to check if you are going down and adjust movment, than some magical timer. Furthermore one raycast is not so expansive, you could even go with spherecast, plus don't let your character to fall down with gravity and adjust movement vector instead or make stairs as ramp.

    In any case this might be helpful: https://catlikecoding.com/unity/tutorials/movement/
     
    Ryiah likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    I would use the normal Raycast because it certainly isn't considered "expensive," especially when you give it a short length equal to the "you stepped down vs you fell off a ledge" distance.

    It's entirely up to you.

    The important point to note is that nobody ever finished a game by writing long paragraphs of text describing what they are contemplating doing. Games get finished by writing code and iterating towards a suitable solution.

    I get it that you're looking or guidance, but generic guidance like this simply doesn't exist. As @Qriva notes above, the solution is going to be EXTREMELY tightly coupled to what kind of game you are making, what behaviour you expect: speeds, feeds, rates, fates, distances, scales, etc.
     
    Ryiah likes this.
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    Ah, but in many games stairs are a slope. A lot of times stairs just have a simple slope collider behind the scenes. For most scenarios, there's no need to deal with all of those individual little steps.

    You can can use IK on a separate layer to place the feet on the actual steps, but even that is not necessary a lot of times because the inaccurate footing is not always noticeable.
     
    Ryiah likes this.