Hovering Ship glued to a Track C#

Hello,

I’m new to unity and i have to use it for a unity project but i’m at a loss with what i need to do Programmaticly i know how to do it but in unity i have no idea if what i have planned would even work and if it would how i get the properties i need.

I currently have my Ship hovering effect by using a force to offset gravity

public float HoverThrust = 9.81f;
private bool hoverPush = false;

void FixedUpdate() 
{	
		if(HoverThrust > 9.85f || HoverThrust < 9.78f){
			hoverPush = !hoverPush;
		}
		HoverThrust = (hoverPush)? HoverThrust+0.02f : HoverThrust-0.02f;
		
		rigidbody.centerOfMass = new Vector3(-0.2f, 0, 0);
		
		Vector3 centralThrusterPosition, centralThrusterForce;
		centralThrusterPosition = new Vector3(
			transform.position.x-0.2f,
			transform.position.y,
			transform.position.z
		);
		centralThrusterForce = new Vector3( 0, HoverThrust, 0);
		
		rigidbody.AddForceAtPosition(centralThrusterForce, centralThrusterPosition);
}

This works and gives me a (mild)hovering effect but as you can see its using gravity force amounts to counter Gravity i want it so i counter gravity by pushing off a track below however it would need to be the track directly below the ship as there will be loop the loops and such

It sounds like you should Raycast downwards from your hovercraft to see if it hits the track. Check out:

The third version of the function, with the layerMask, would allow you to only test for collisions against a ‘track’ layer that you could set up. This would avoid any interference with other objects in your scene that shouldn’t be used as hover platforms.

Assuming you hover engines have a maximum range of 10 meters (could be anything!), the raycast call would be something like:

Physics.Raycast(transform.position, Vector3.down, 10, layerMask)

If you want to check ‘downwards’ based on the vehicles orientation use:

Physics.Raycast(transform.position, -transform.up, 10, layerMask)

If you then want to apply the hover force based on the vehicles orientation calculate the force to apply like this:

Vector3 hoverForce = transform.up * HoverThrust;

If you want to check for the track existing under a larger area of the vehicle you could either run multiple Raycasts at the extents of your vehicle or use a SphereCast with a reasonable radius.

By collecting the RaycastHit (the second set of three Raycast methods) you can also apply the thrust force based on the track orientation:

Vector3 hoverForce = hitInfo.normal * HoverThrust;

Either of these two options will cause the vehicle to slide down and thrust sideways when either the vehicle or track aren’t perfectly horizontal. You could either allow this as a cool effect or counter it by fixing the Y thrust to 9.81.

If you want to make the hover thrust force proportional to the distance from the track you could use the the Raycast methods that give HitInfo to get the distance, then use whatever formula you want to calculate the force. This could give you a very easy way to make the vehicle rise from the track to the desired hover height… a very simple example for a desired hover height of 2 (higher than gravity force close to the track, less than gravity force further than 2 meters away from track):

float hoverThrust = 9.81f * 2/Mathf.Clamp(hitInfo.distance, 0.5f, 4f);