Hovercraft Physics in Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hover_Forces : MonoBehaviour
{
#region Variables
public float maxSpeed , upForce , forwardForce , torqueSpeed;
private Rigidbody hoverRB;
[Header("DragProperties")] // limit the force Bounds
public float dragFactor;
[Header("Weight Properties")]
public float weightInLBS = 10f;
public float weight;
const float lbsToKG = 0.454f;
const float kgToLBS = 2.205f;
#endregion
#region Methods
// Start is called before the first frame update
void Start()
{
hoverRB = GetComponent<Rigidbody>();
float finalKG = weightInLBS * lbsToKG; // local variable to calculate the Final weight
weight = finalKG; /// final Weight Value in KG
if (hoverRB)
{
hoverRB.mass = finalKG;
}
}
// Update is called once per frame
void FixedUpdate()
{
if (hoverRB)
{
if (Input.GetKey(KeyCode.Space))
{
hoverRB.AddForce(Vector3.up * upForce * Time.deltaTime);
}
if (Input.GetKey(KeyCode.W))
{
hoverRB.AddForce(Vector3.forward * forwardForce * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A))
{
hoverRB.AddForce(Vector3.left * forwardForce * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
hoverRB.AddForce(Vector3.right * forwardForce * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
hoverRB.AddForce(Vector3.back * forwardForce * Time.deltaTime);
}
hoverRB.AddTorque(Vector3.up * torqueSpeed * Time.deltaTime);
DragMethod();
}
}
public void DragMethod() /// force to inbound to make a lag between the object /// create Slomo effect
{
if (hoverRB)
{
float currentSpeed = hoverRB.velocity.magnitude;
float finalDrag = currentSpeed * dragFactor;
hoverRB.drag = finalDrag;
}
}
#endregion
}//class