Hovercraft em unidade
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<Rigidbody>();
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
Sore Serval