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.
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.
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 :)
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.
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.
@@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
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 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.
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);
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?
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
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.
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!
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?
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.
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.
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?
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
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 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.
@@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)
@@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
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.
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.
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 :)
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.
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.
@@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
Thank you!! This was my second attempt at this and your explanation of the forces at play here was really helpful.
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.
Is that what you implemented on your hover racer prototype? It looked good!
@@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.
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);
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?
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
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.
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.
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!
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?
My item keeps flying into the air. Does this require Gravity on the Rigid Body?
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.
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.
Very good!!!
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?
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
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??
You, Sir, are an absolute wizard.
Is there a way to adjust the hover height?
Yes. All you have to do is change the length of the spring.
@@bitgalaxis1147 i changed the length field to 2.0 with no visible change. 3.0 made the entire thing take off and fly away.
@@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.
@@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)
@@bitgalaxis1147 thank you, I'll try to adjust it and see what happens.
Please trying to implement this but it never stabilizes
Be sure to check if you have dampening, and play with dampening values, or add drag.
@@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
Could you post the code to github so we can download it?
Is there a way to make hovering enemys?