Hovercraft Physics in Unity
///// Hovercraft Physics Beahaviour
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HoverBehaviour : MonoBehaviour
{
#region Variables
private Rigidbody hoverRB;
private float weight;
private float currentGroundDistance;
[Header("Hover Propoerties")]
public float hoverHeight = 3f;
public Transform hoverPos;
#endregion
#region Methods
// Start is called before the first frame update
void Start()
{
hoverRB = GetComponent();
if (hoverRB)
{
weight = hoverRB.mass * Physics.gravity.magnitude; // Calculateed the wight of the Object
}
}
// Update is called once per frame
void FixedUpdate()
{
if (hoverRB & hoverPos)
{
CalculateGroundDistance();
HandleHoverForce();
}
}
public void CalculateGroundDistance() // Raycast Distance Check
{
Ray hoverRay = new Ray(hoverPos.position, Vector3.down);
Debug.DrawRay(hoverPos.position, Vector3.down,Color.red);
RaycastHit hit;
if(Physics.Raycast(hoverRay,out hit, 30f))
{
if(hit.transform.tag == "Ground")
{
currentGroundDistance = hit.distance;
}
}
}
public void HandleHoverForce() /// handle Hover Spring Bounce
{
float groundDifference = hoverHeight - currentGroundDistance; //// local float variable that can calculate the distence and according weight
Vector3 finalHoverForce = Vector3.up * (1 + groundDifference); /// To Upwards
hoverRB.AddForce(finalHoverForce * weight); /// float to help in the Mid air /// behave like Spring
}
#endregion
}///class
Hovercraft Physics in Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HoverBehaviour : MonoBehaviour
{
#region Variables
private Rigidbody hoverRB;
private float weight;
private float currentGroundDistance;
[Header("Hover Propoerties")]
public float hoverHeight = 3f;
public Transform hoverPos;
#endregion
#region Methods
// Start is called before the first frame update
void Start()
{
hoverRB = GetComponent();
if (hoverRB)
{
weight = hoverRB.mass * Physics.gravity.magnitude; // Calculateed the wight of the Object
}
}
// Update is called once per frame
void FixedUpdate()
{
if (hoverRB & hoverPos)
{
CalculateGroundDistance();
HandleHoverForce();
}
}
public void CalculateGroundDistance() // Raycast Distance Check
{
Ray hoverRay = new Ray(hoverPos.position, Vector3.down);
Debug.DrawRay(hoverPos.position, Vector3.down,Color.red);
RaycastHit hit;
if(Physics.Raycast(hoverRay,out hit, 30f))
{
if(hit.transform.tag == "Ground")
{
currentGroundDistance = hit.distance;
}
}
}
public void HandleHoverForce() /// handle Hover Spring Bounce
{
float groundDifference = hoverHeight - currentGroundDistance; //// local float variable that can calculate the distence and according weight
Vector3 finalHoverForce = Vector3.up * (1 + groundDifference); /// To Upwards
hoverRB.AddForce(finalHoverForce * weight); /// float to help in the Mid air /// behave like Spring
}
#endregion
}///class
Hovercraft Physics in Unity
////// Hovercraft Physics Behaviour
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HoverBehaviour : MonoBehaviour
{
#region Variables
private Rigidbody hoverRB;
private float weight;
private float currentGroundDistance;
[Header("Hover Propoerties")]
public float hoverHeight = 3f;
public Transform hoverPos;
#endregion
#region Methods
// Start is called before the first frame update
void Start()
{
hoverRB = GetComponent();
if (hoverRB)
{
weight = hoverRB.mass * Physics.gravity.magnitude; // Calculateed the wight of the Object
}
}
// Update is called once per frame
void FixedUpdate()
{
if (hoverRB & hoverPos)
{
CalculateGroundDistance();
HandleHoverForce();
}
}
public void CalculateGroundDistance() // Raycast Distance Check
{
Ray hoverRay = new Ray(hoverPos.position, Vector3.down);
Debug.DrawRay(hoverPos.position, Vector3.down,Color.red);
RaycastHit hit;
if(Physics.Raycast(hoverRay,out hit, 30f))
{
if(hit.transform.tag == "Ground")
{
currentGroundDistance = hit.distance;
}
}
}
public void HandleHoverForce() /// handle Hover Spring Bounce
{
float groundDifference = hoverHeight - currentGroundDistance; //// local float variable that can calculate the distence and according weight
Vector3 finalHoverForce = Vector3.up * (1 + groundDifference); /// To Upwards
hoverRB.AddForce(finalHoverForce * weight); /// float to help in the Mid air /// behave like Spring
}
#endregion
}///class
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();
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
|