- Home /
How to face Hovercraft physics
Hi, I'm trying to get a smooth and fluid physics for an hovercraft race game, something similar to this:
I writed a script for that but it wasn't very smooth and I decided to restart it with another perspective.
What I tried for now was a rigidbody with gravity enabled and using raycast check the collision with the ground and apply a Vector3.up which keeps the distance with the ground.
The problem is that when I go forward and detect a ramp or a hill it's really slow on going up.
My question is how to get a good behaviour with an hovercraft?
Also when turning I want the hovercraft to rotate to the side a little, would I need to do this with animations or should I rotate it on the local axis and use world axis for the movement?
I'm a little messed so if you need more information I will provide it.
Regards.
there are probably quite a lot of ways of doing this. perhaps you should write a little bit of code and post it here to get suggestions or directions. otherwise you're asking people to write your code for you from scratch.
you're more likely to get help if you start off with some sample code
@zombience Sure, I'll post it when I arrive home, but my question is oriented to get ideas on how to get it not the code itself. In example, attach a raycast to each corner of the model and check the distance with the ground, just one on the center, etc...
Anyway I will post my code in a while. Thank you.
I've read through a few UA post about hovercrafts. According to the posts the "best" results are obtained by pushing a collider across the ground (or a first person controller with momentum) and attaching the visible craft using a joint...like the craft in the picture was really run across the ground using another vehicle and a boom. According to the posts, this solution does much better at the turning and climbing than a pure force model. Couldn't find the posts on a quick search, but if you Google around a bit, they will turn up. There are also a number of posts implementing a pure force model.
Answer by TheDarkVoid · May 14, 2013 at 10:54 PM
I'm kindof reluctant to tell you this information since i'm also doing a hovercar racing game but maybe it can be improved. So here is the script i used to achive a really good hovering effect, just not that easy to apply to exotic vehicles.
public List<Transform> HoverPoints = new List<Transform>();
public float HoverHeight = 7;
public float HoverForceFront = 200;
public float HoverForceBack = 400;
void FixedUpdate ()
{
//Lift
for(int i=0; i < 4; i++)
{
RaycastHit Hit;
if(i >1)
{
if(Physics.Raycast(HoverPoints[i].position, HoverPoints[i].TransformDirection(Vector3.down), out Hit, HoverHeight))
thisRigidbody.AddForceAtPosition((Vector3.up * HoverForceBack * Time.deltaTime)* Mathf.Abs(1-(Vector3.Distance(Hit.point, HoverPoints[i].position)/ HoverHeight)), HoverPoints[i].position);
if(Hit.point != Vector3.zero)
Debug.DrawLine(HoverPoints[i].position, Hit.point, Color.blue);
}else
{
if(Physics.Raycast(HoverPoints[i].position, HoverPoints[i].TransformDirection(Vector3.down), out Hit, HoverHeight))
thisRigidbody.AddForceAtPosition((Vector3.up * HoverForceFront * Time.deltaTime)* Mathf.Abs(1-(Vector3.Distance(Hit.point, HoverPoints[i].position)/ HoverHeight)), HoverPoints[i].position);
if(Hit.point != Vector3.zero)
Debug.DrawLine(HoverPoints[i].position, Hit.point, Color.red);
}
if(Hit.point != Vector3.zero)
isGrounded = true;
else
isGrounded = false;
}
}
It uses four points that you define in the inspector that act as points of lift, it applys a weak and strong force on the front/back to help balance vehicles, it's also set up in a way that prevents the vehicle from sliding down ramps. thisRigidbody is a private variable that holds an instance of the gameObject's rigidbody component.
This is a video of it in action, with a bit of other stuff. I also made a hovercraft script that used a character controller that slid across the ground, it's a but easier but a lot more limiting here is a video of that.
Hi all, sorry for the delay.
This is my approach to this problem:
#pragma strict
public var floatSpeed = .5;
function Update() {
if (Physics.Raycast (transform.position, Vector3.down, 5.0)) {
rigidbody.transform.position += Vector3.up * floatSpeed * Time.deltaTime;
rigidbody.drag = 20.0;
}
else{
rigidbody.drag = 1.0;
}
}
The ray has the distance I want to the ground and when there is no colision the hovercraft falls with the gravity, but when there is colision the drag is increased so the fall is slow and a vector up force is applied to keep the distance. The problem with this is that I want a spring effect and my solution does not simulate it.
@TheDarkVoid Thank you for the code, I addapted it to my game but it's not very stable, at least for me, and it requires a little bit of tunning with the position of the hoverpoints to stabilize it.
Now, I'm trying some different approaches and testing which one gives me the best result.
Regards.
$$anonymous$$y script allows you to modify the force on the front and back of the hovercraft independently to allow for increased stability. It also better if the hover points are symmetric on the left and right and also dristibuted base on the shape of your mesh, since unity's physics center of mass is deter$$anonymous$$ed by the volume of the mesh. ThIs results in it being harder to to create a script that will work for any model.
i tried that with constant force, if the ship went fast to the ground from high it would bounce miles high as it was bouncy force, and it was challenging to see forwards at hills arriving.
@ZoomDomain At the moment I'm trying different approaches. Also I was testing the script posted by TheDarkVoid, but it doesn't convince me. When I go fast to a ramp the up force is not enough to raise the hovercraft and it slides in it.
I tried using the constant force but it doesn't seem real. Now I'm testing it with my own gravity but I didn't find a good result for now.
$$anonymous$$y version was very newbie, it ended up having 10 different kinds of forces that could take over from each other in different speed and height combinations, and it was unsatisfactory so i ditched it. it was putting patches to fix patches and it wasnt easy or nice solution.
if i did it again, i think i would use an invisible larger square underneath the craft, extending some distance around it, and send raycasts down from each corner and compare see if any is near a wall. perhaps extend the square in a function of distance, so at 100kph you send raycasts from areas 100 meters in front, and going slow the square becomes smaller.
the only thing i just about managed was the self writing system that flips the craft straight so that it can tumble 3-5 rolls when it hits something and go flat again.
ill see if i can dig out the code. it's using the principle that upwards constant force is logarithmically proportional to distance from ground, when to close it pushes real fast, and that made it bounce alot. but when it was idle the craft would hover up and down like it was on a swim$$anonymous$$g pool it was cute.
Answer by MountDoomTeam · May 24, 2013 at 11:28 AM
Alucard i just watched video 1 it was great, although it was difficult to listen as he was not clear about the mechanism used... he used quaternion for rotation, so the craft doesnt have rigidbody physics, and what did he do with the 4 corners of the craft??? measure height only? i didnt get it.
also... wanted to mention that hovercraft is same as having car with invisible wheels, and some suspension... to prevent bouncing as crazy, reverting to car with invisible wheels mode could be pretty handy.
@ZoomDomain Hi, I'm trying another method.
I'm starting by moving a sphere with forces over the map, once the movement and physics are correct I'm going to use a hinge joint attached to the center of the sphere and my model so it can simulate de movement on rotation, aceleration and deceleration. For the floating effect I'm not sure by now but I think I'll use a premade animation for that when the ship is in idle state.
Answer by alex107 · Sep 01, 2013 at 01:55 AM
@TheDarkVoid You should add a center of mass variable to the code.
public Transform CenterofMass; public bool isGrounded; // Use this for initialization void Start () { thisRigidbody.centerOfMass = CenterofMass.position - thisRigidbody.position; Debug.Log(CenterofMass.position - thisRigidbody.position); }
Hello, everybody. Can you help me, please. I have the error in line: thisRigidbody.AddForceAtPosition((Vector3.up hoverForceBack Time.deltaTime) * $$anonymous$$athf.Abs(1-(Vector3.Distance(hit.point, hoverPoints[i].position) / hoverHeight)), hoverPoints[i].position); Console message: NullReferenceException: Object reference not set to an instance of an object
Answer by claasmi · May 29, 2015 at 09:16 AM
The key is to use damping. Simply apply a force linear to the vertical velocity in the opposite direction.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Spaceship physics - adding resistance 2 Answers
Scripting "physics"? 1 Answer
2.5D Platformer - Jump Question 1 Answer
Vector3.Reflect problems 4 Answers