Horje
hovercraft in Unity Code Example
hovercraft 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<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





Csharp

Related
largest element in array in c# Code Example largest element in array in c# Code Example
nodatime instant to datetime off set c# Code Example nodatime instant to datetime off set c# Code Example
c# regex double of some letters only Code Example c# regex double of some letters only Code Example
how to store some variables on the device in unity Code Example how to store some variables on the device in unity Code Example
how to mock http client c# Code Example how to mock http client c# Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
8