Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Hover Car realistic.

Discussion in 'Scripting' started by EdgarCarrera, Aug 7, 2016.

  1. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    Hello im trying to make a hover car realitic but i got much jumping or hover force then. can u tell me how to optimice? i wanna to make like this:



    this is my wheel code for hover effect:


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class HoverEngine : MonoBehaviour
    4. {
    5.     public LayerMask RaycastMask;
    6.     public float MaxHeight=2;
    7.     public float GroundForce=4000;
    8.     public float Damping=2;
    9.     public float Exponent=2;
    10.     public float MaxAngleDrift=10;
    11.     public Rigidbody Rigidbody;
    12.  
    13.     private float m_LastPower;
    14.     private RaycastHit m_GroundHit;
    15.     public bool HasGround
    16.     {
    17.         get { return m_LastPower > 0; }
    18.     }
    19.     public float Power
    20.     {
    21.         get { return m_LastPower; }
    22.     }
    23.     public RaycastHit Ground{get { return m_GroundHit; }}
    24.  
    25.     void OnDrawGizmos()
    26.     {
    27.         //  Hover Force
    28.         RaycastHit hit;
    29.  
    30.         if (Physics.Raycast(transform.position, -transform.up, out hit, MaxHeight, RaycastMask))
    31.             {
    32.                 Gizmos.color = Color.blue;
    33.                 Gizmos.DrawLine(transform.position, hit.point);
    34.                 Gizmos.DrawSphere(hit.point, 0.2f);
    35.             }
    36.             else
    37.             {
    38.                 Gizmos.color = Color.red;
    39.             Gizmos.DrawLine(transform.position, transform.position - transform.up * MaxHeight);
    40.             }
    41.  
    42.     }
    43.  
    44.     void FixedUpdate()
    45.     {
    46.         // find force direction by rotating local up vector towards world up
    47.         var up = transform.up;
    48.         var grav = Physics.gravity.normalized;
    49.         up = Vector3.RotateTowards(up, -grav, MaxAngleDrift*Mathf.Deg2Rad, 1);
    50.  
    51.         // check if we see ground below
    52.         m_LastPower = 0;
    53.         if (!Physics.Raycast(transform.position, -up, out m_GroundHit, MaxHeight, RaycastMask))
    54.         {
    55.             return; // no ground - no hover
    56.         }
    57.  
    58.         // calculate power falloff
    59.         m_LastPower = Mathf.Pow((MaxHeight - m_GroundHit.distance)/MaxHeight, Exponent);
    60.         var force = m_LastPower*GroundForce;
    61.  
    62.         // calculate damping, which is proportional to square of engine upward velocity
    63.         var v = Vector3.Dot(Rigidbody.GetPointVelocity(transform.position), up);
    64.         var drag = -v*Mathf.Abs(v)*Damping;
    65.  
    66.         // add force and damping. Note that force is added at engine position
    67.         Rigidbody.AddForceAtPosition(up*(force + drag), transform.position);
    68.     }
    69. }
    70.  
     
  2. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
  3. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    WheelCollider
     
  4. EdgarCarrera

    EdgarCarrera

    Joined:
    Apr 21, 2014
    Posts:
    255
    WheelCollider sucks.
    i want use raycast more realistic. wheel colider have bad car controll.