Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Discussion Grounding a Player/Object using Raycasts or Physics?

Discussion in 'Physics' started by Zetric, Jul 27, 2022.

  1. Zetric

    Zetric

    Joined:
    Apr 2, 2013
    Posts:
    3
    Hey there! I need some serious help with grounding my character.

    First off, I am a hobbyist unity user. I'd consider myself having an intermediate skill level in C# and unity as a whole. Currently I'm trying my best to code a very functional "kinematic" character controller using finite state machines and other such implementations. I have a pretty good understanding of how to make everything work together and I got the plan more or less mapped out in a flowchart.

    What I have been struggling with for the longest time, is grounding my character!
    I cannot for the life of my figure out the best way to accomplish this, so that it works with slopes, cliffs and other edge case senarios.

    The awnser to this will probably not be "check the api for spherecasting", as the issue is far greater than something like that. Seeing as I want to code this controller by myself to do only what I want it to. I kind of refuse to use a rigidbody or the built in character controller on my player.

    Methods I have tried so far:
    • Spherecasting
      I have been trying to use spherecasts for the longest time, but the normals are not good, since for example, the normal on the edge of say a step of a staircase is reported "wrongly" in my opinion.
      Compensating for this by using additional raycasts, can give wonky results.
      Compensating with additional boxcasts cant guarantee that you hit the same collider. Even if you do, if its any complex mesh collider. since you cant hit it multiple times u might hit the completely wrong part of it.
    • Boxcasting
      I have tried using boxcasts since they report better normals, but I end up having to use at least 3 simultanious boxes to emulate the shape of a sphere/cylinder, which would be optimal for a character shape.
      On top of those 3 casts, I also have to do additional casts to check for proper ground when hitting a sloped surface and even more for a cliff and also for "pebbles" on the ground which might report an incorrect normal, but is so tiny the cast should ignore it.
    • Physics.ComputePenetration
      I have also tried using the ComputePenetration method to find a good normal and position on the ground, but getting the vertical position of a depenetrated sphere/box seems unreasonably hard, finding valid depenetration-normals also proves difficult.
    • Predictive raycasting
      I have also tried a "complex" prediction path of many ray/spherecasts from intersection to intersection, casting both forwards and downwards, calculating positions and normals etc.
      Results are unsatisfying unfortunately.
    • Mesh "manipulation"
      In the abcence of correct normals for spherecasts, I have also tried a method of uncovering each triangles normal in a mesh and finding the closest one to a sphere and using that as the ground.
      As u could imagine, its probably way too expensive and a clusterfck of information to try and digest and use reasonably in the project when I need ground information for other methods and such.
    This turned out to be a little bit of a ramble, but I really am stumped. I have been at this on and off for weeks. But I just cant find a good solution.

    Anyone with tips, guidance or link directions are more than welcome to share!
    It really baffles me, how it can be this hard to do something so simple as keeping a character on the ground and extracting proper values from where he stands. But who knows, maybe its just me being stupid?

    If I eventually find a good solution I will make a proper post about it with gif's and code examples.
     
  2. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    Not to be rude or perhaps unhelpful, but why not just modify an asset/built-in solution to your needs? Remove what you don't need, optimize a thing or two...
     
  3. Zetric

    Zetric

    Joined:
    Apr 2, 2013
    Posts:
    3
    Not being rude at all :)
    I understand that it would be a pretty good solution to the issue. But im also using this as experience to gain a better understanding of code and unitys systems.
    I guess what im fishing for is some magic method that sort of solves the problem.

    For instance, fairly recently we got access to the compute penetration method. Which helps alot in separating colliders without the use of rigidbodies or having to call the oncollisionenter methods.

    Recently figured out how to manipulate gizmo debugs with rotation. A simple localToWorldVector * matrix4x4.Rotate(euler) function that wasn't too obvious at first glance.
    Or how easy it is to rotate a normal vector by simply multiplying it with a quaternion euler.

    Or how I found sweptests of a rigidbody to be able to "raycast" a custom collider shape.

    Im just hoping there is some form of contact points+normals or avarage ground heightmap something method or common practice that would be useful in determining a proper ground position, slope and edge point. That I simply have missed when trying to find awnsers.

    Seeing as how grounding a character should be one of the very first steps in making pretty much any game I'd assume there would be some common practice for making it work smoothly.

    Im surprised actually, with how entry level and available to the public unity is. All its amazing rendering capabilities, visual scripting etc. There isn't anything that solves or helps with such a basic task already. Along with many other pretty basic needs in theory.A nd barely any information from anyone who has figured it out. All I find is almost 10 year old threads laying out the same issue over and over, with no real conclusion with a good result. All of it is workarounds, and patchwork "good for now" results.

    Sorry for more rant, im kinda passionate about finding a solution to this.
     
    gjaccieczo likes this.
  4. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    No worries, rants are a part of conversation and i completely understand that. I was thinking of just dropping some asset suggestions, but it seems that you're more on the "learn and understand Unity" path rather than "complete a project and forget about it" (which is good, as this path allows you to do the "complete and forget" much quicker as a result ;)).
    I understand that asking more questions is usually underwhelming, especially if you seek answers, but could you just perhaps bring an example of what you're trying to achieve? The problem is (from my perspective) that for example my definition of "grounding" the player is basically throw in some colliders and rigidbodies, find a controller and done. A more advanced approach would be say a players foot changing its angle according to the angle of the surface it is on. Both are going to work with slopes and cliffs but with varying degrees of quality.
    Perhaps there is a game that has the "grounding" logic that you could bring up as an example of what you're trying to achieve. Before making my comment, i wanted to direct you to Gigaya (now a canceled Unity demo project) as the "grounding" logic seemed to be quite powerful there. Still, check out the demo video, as the way the player seems to be "grounded" is quite impressive (at least to me).