Hover Physics in Unity3D

Поделиться
HTML-код
  • Опубликовано: 1 ноя 2024

Комментарии • 36

  • @TrutaDeko
    @TrutaDeko 3 года назад +6

    Over at the Unity channel they have a hover racer tutorial where it's mentioned the use of a PID controller to stabilize the ship. It seems like it is commonly used for stuff like self-driving vehicles. Your video is the second tutorial on hovering mechanics I've watched so far, besides the Unity one, that tries to tackle the fluctuation problem, and is also the best presented.

    • @bitgalaxis1147
      @bitgalaxis1147  3 года назад +1

      Thank you for the feedback! The primary reason I made this video was my own frustration not being able to stop the bouncing when implementing hover systems shown elsewhere. My best guess is others are using drag on their rigidbodies to stop the oscillations.

    • @isobelshasha2095
      @isobelshasha2095 3 года назад

      The key element is the "D" in PID- adding a force based on the derivative of the spring length (aka how fast the length of the spring is decreasing, aka damping). So, these are just slightly different ways of describing the same approach :)

  • @Backtothegameplay
    @Backtothegameplay 3 года назад +2

    This is an incredible tutorial. Probably the best one I've seen about the topic so far. The problem is: Depending on scale of your objects, gravity, mass and drag, things like "a lot of strength" and "most extreme dampening" become more ambiguous than they are already. After an hour of playing with the variables I'm just swinging between "never stabilizes" and "shoots away like a bullet as soon as angle of ground changes". Hitting that soft spot where the dampening and strength of the spring becomes just right to react to changes in the environment really is an exercise in frustration.

    • @bitgalaxis1147
      @bitgalaxis1147  3 года назад

      You have a good point. One improvement from this video I found was setting the dampening dependent on the spring strength, but there are a lot of different approaches. One of the easy "fixes" is to fake dampening by increasing drag on the rigidbody, but that affects all motion and not just the hover effect. The PID controller actually is a better solution I think.

    • @Backtothegameplay
      @Backtothegameplay 3 года назад

      @@bitgalaxis1147 I believe to think so, too. I am working with really small drag values and was aiming to have as little as possible or none at all to get really friction-less movement, but that does make a lot of simple hover solutions a very explosive mix. :D

  • @Krwatkins0002
    @Krwatkins0002 2 года назад +2

    Thank you!! This was my second attempt at this and your explanation of the forces at play here was really helpful.

  • @bufferoverflovv
    @bufferoverflovv 3 года назад +6

    implementing a minimum and maximum spring travel length can help with bounce/oscillations overshooting so the dampening can work faster to bring the springs back a normal position.

    • @bitgalaxis1147
      @bitgalaxis1147  3 года назад +4

      Is that what you implemented on your hover racer prototype? It looked good!

    • @bufferoverflovv
      @bufferoverflovv 3 года назад +7

      @@bitgalaxis1147 It's basically the same thing that you have implemented with Hookes Law, I've just added the rest length and travel length and clamped them to a minimum (rest length - travel length) & maximum (rest length + travel length). There's a great Khan Academy resource (Spring forces) that goes through it. You can actually also add some velocity to the dampening by dividing it by Time.deltatime. (lastHitDist - hitDistance) / Time.deltatime.

  • @javierherasalcuaz
    @javierherasalcuaz 2 года назад

    Thats AMAZING! easy, fast and effective! Thank you very much, i will use for my car for sure.
    I've made fast changes to avoid huge numbers on forces when youre using a small lenght:
    float floorDistanceNormalized = floorDistance / springLength;
    float lastFloorDistanceNormalized = lastFloorDistance / springLength;
    float forceAmount = springForce * (1 - floorDistanceNormalized);
    forceAmount += dampingForce * (lastFloorDistanceNormalized - floorDistanceNormalized);

  • @lunaplays2960
    @lunaplays2960 7 месяцев назад +1

    I am trying to make a character hover however I'm using 2D and cant find any tutorials for a 2D version. Does anyone know code that would work best with the 2D assets?

  • @r8Rusoski
    @r8Rusoski Год назад

    I am using 2 PID controllers to:
    1) Make the ship hover above the desire distance by applying force
    2) Make the ship align to the normal vector of the face bellow it by applying torque
    This works fine for slow speed hovercrafts (bellow 300km/h), however I am struggling to make it work for high speed hovecrafts (above 600km/h), hard turns make the ship collide

  • @NoiseChampionBlack
    @NoiseChampionBlack 3 года назад +3

    Is it possible you could show how exactly you are able to do the multiple raycasts? I'm still new and having issues with it.

    • @bitgalaxis1147
      @bitgalaxis1147  3 года назад +3

      Oh sure. So what I'm actually doing is creating a prefab, which is just 1raycast, and attaching it to the truck prefab. Then I manually override the default values.
      So basically, it's just 1 script and object, and I attach 4 of them to each truck. There's no additional logic for extra. When you need more, you add more. I can cover that in a video if it would help. Just let me know.

    • @NoiseChampionBlack
      @NoiseChampionBlack 3 года назад +1

      I appreciate the reply! I'll give that an attempt by making a prefab for raycasts. And it's up to you if you want to make a video, I appreciate the response all the same!

  • @brain_sushii7632
    @brain_sushii7632 Год назад

    When I try adding the first code to show the hover effect, my object just flies off in to the void upwards, I have rigidbody and gravity there, any reason why that would be?

  • @hunterfox6230
    @hunterfox6230 6 месяцев назад

    My item keeps flying into the air. Does this require Gravity on the Rigid Body?

  • @Alzexza
    @Alzexza 3 года назад +3

    How did you get your vehicles to self-right when tipping over?
    would love to see the code.
    Might want to show how you derive your c*v + K*x = F... but including the last hit distance, you effectively creating a pseudo CV term.

    • @bitgalaxis1147
      @bitgalaxis1147  3 года назад +2

      There is no self-righting code on this, as the 4 springs on each truck generally work well to self-correct. If I disable some of the springs, the vehicles will flip.
      You are correct that the last hit distance is a pseudo CV term, because it's a cheat -- to quote the video, "This is a quick dirty calculation." Dirty needs more emphasis. The last hit distance is only being used to approximate the speed with which the spring is being compressed in order to simulate a dampening effect. It's not correctly calculating dampening forces because it would require an evaluation of the relative speed between two objects. This cheat is close-enough, simple, and had better results (for my purposes) than actually implementing speed calculations.

  • @Massive-3D
    @Massive-3D 3 года назад +1

    Very good!!!

  • @charliejusko1625
    @charliejusko1625 Год назад

    Great tutorial! Having some issues with collisions though. I attached the code to a model with some controls and can hover around. But the character goes right through any collider ABOVE the height of the raycast position for the hover. I have capsule colliders on both the characters and the stationary objects in question. The collision is detected (had some debug in the OnCollision methods) so I know they are colliding. But the character just rides straight through them. How did you manage the collision detection in the video here?

    • @charliejusko1625
      @charliejusko1625 Год назад

      I should say, if the object is under the raycast position, the hover will adjust accordingly and go over the object no problem. It seems to only be an issue with collisions that are above the raycast position

  • @takumifujiwara2073
    @takumifujiwara2073 2 года назад

    amazing tutorial. there should be more vids like this, ones that actually explain what’s going on, instead of expecting u to copy and leave u to figure out how it works. thank you!!!🙏🙏🙏🙏 one thing tho, how do you lower the height?? with the ball, if you lower the gravity force it will fall, at different rates but fall nonetheless. how did you make the second example hover at a lower height??

  • @ZefugiLive
    @ZefugiLive 3 года назад +3

    You, Sir, are an absolute wizard.
    Is there a way to adjust the hover height?

    • @bitgalaxis1147
      @bitgalaxis1147  3 года назад +2

      Yes. All you have to do is change the length of the spring.

    • @ZefugiLive
      @ZefugiLive 3 года назад +1

      @@bitgalaxis1147 i changed the length field to 2.0 with no visible change. 3.0 made the entire thing take off and fly away.

    • @bitgalaxis1147
      @bitgalaxis1147  3 года назад +1

      @@ZefugiLive That's odd. I would make sure the starting position of the object is slightly beyond the length, but it shouldn't matter. Also make sure there's a max force. I'll try to reproduce it.

    • @bitgalaxis1147
      @bitgalaxis1147  3 года назад +1

      @@ZefugiLive Ok I played with some settings and recognize what the problem might be. The formula used is very simplified, but there are a lot of factors we don't consider in this video that need to be addressed for edge cases.
      To keep it simple, be careful with the combination of the total mass, spring length, and spring strength, and even dampening (in reality, dampening shouldn't even be a variable, it should just be based on the strength, but still something the developer should tweak). The lower your mass, the weaker your springs should be. The shorter the spring, the less stable it will be for small movement. With extremely long springs, going over extremely uneven landscape or (accidentally toppling the object) can make the physics go haywire.
      Here are some ideas I've played with:
      1. Adding several more springs (some angled outward at the edges by around 15 degrees) and reducing their power and dampening (this can really smooth out the motion if done right but takes a bit of tweaking)
      2. Just playing with the weight, length, strength, and dampening.
      3. Modifying the code to output a single force (this one is has smoother up and down motion, but less control over the vehicle rolling over)

    • @ZefugiLive
      @ZefugiLive 3 года назад +1

      @@bitgalaxis1147 thank you, I'll try to adjust it and see what happens.

  • @iammrwaynee
    @iammrwaynee 4 года назад +2

    Please trying to implement this but it never stabilizes

    • @bitgalaxis1147
      @bitgalaxis1147  4 года назад +3

      Be sure to check if you have dampening, and play with dampening values, or add drag.

    • @iammrwaynee
      @iammrwaynee 4 года назад +2

      @@bitgalaxis1147 I do then it stabilizes but once I try to even use it on a procedural animation, it goes haywire but trying to implement a PID controller. Thanks

  • @stephenftw7
    @stephenftw7 2 года назад

    Could you post the code to github so we can download it?

  • @innovativename5268
    @innovativename5268 3 года назад +2

    Is there a way to make hovering enemys?