Hi there,
I’ve created a hover vehicle that’s simulating a lot of physics functions. For the most part it works well but I’m running into issues at vertical angles. For example when the hovercraft reaches an angle of 85 degrees to 90 degrees the hover force stops working and the gravity force just kicks in. So the hover force does not work at vertical angles and I’m wondering it’s something to do with AddForceAtPosition
The physics work similar to FZero where the vehicle sticks to the track mesh. Couple things to note, I’m not using the rigid body gravity.
I’ve added a code block so you can see main part of the code where it’s happening.
I’ve added images that describe the issues I’m having.
Something else to note is if my ship is upside down I have AddForceAtPosition to the negative of the transform.up to get the ship to hover upside down.
private void ShipGravity()
{
//Apply Gravity
if (this.shipGrounded) {
this.shipGravity = -transform.up * this.settings.shipGravity_Low * 0.6f;
}else
{
this.shipGravity = Vector3.down * this.settings.shipGravity_Low;
}
//Apply gravity to down
this.shipBody.AddForce(this.shipGravity, ForceMode.Acceleration);
//Hover Points
List<Vector3> HoverPoints = new List<Vector3>
{
transform.TransformPoint(-settings.shipSize.x * 0.5f, 0.0f, settings.shipSize.z * 0.5f),
transform.TransformPoint(settings.shipSize.x * 0.5f, 0.0f, settings.shipSize.z * 0.5f),
transform.TransformPoint(settings.shipSize.x * 0.5f, 0.0f, -settings.shipSize.z * 0.5f),
transform.TransformPoint(-settings.shipSize.x * 0.5f, 0.0f, -settings.shipSize.z * 0.5f)
};
//Faling Dampener
if (this.shipGrounded)
{
this.fallingDamper = Mathf.Lerp(this.fallingDamper, 0.0f, Time.deltaTime);
}
else
{
this.fallingDamper = Mathf.Lerp(this.fallingDamper, 0.1f, Time.deltaTime);
}
//Go through each point to make ship hover
this.shipGrounded = false;
for (int i = 0; i < HoverPoints.Count; i++)
{
int layerMask = 1 << LayerMask.NameToLayer("TrackFloor");
RaycastHit raycastHit;
if (Physics.Raycast(HoverPoints[i], -base.transform.up, out raycastHit, settings.shipRideHeight, layerMask))
{
//MeshNormal = new Vector3 (raycastHit.normal.x, raycastHit.normal.y, raycastHit.normal.z);
//MeshAngle = Vector3.Angle (MeshNormal, Vector3.up);
shipGrounded = true;
hoverForce = settings.shipRideHeight - raycastHit.distance; //float (Note that the ride height is 5.5)
spring = HoverPoints[i] - raycastHit.point; //Vector3
length = raycastHit.distance; //float
displacement = length - settings.shipRideHeight; //float
springN = spring / length; //Vector 3
compression = (settings.shipRideHeight / raycastHit.distance - 1) * -1; //float
restoreForce = springN * (displacement * (hoverForce * (-10 + (compression * 3)))); //Vector3
force = restoreForce.y; // float
force -= shipBody.GetPointVelocity(HoverPoints[i]).y * ((0.3f) + fallingDamper);
shipBody.AddForceAtPosition (transform.up * force, HoverPoints [i], ForceMode.Acceleration);
groundDistance = raycastHit.distance;
Debug.DrawRay (HoverPoints[i], -transform.up * groundDistance, Color.green);
}
}