Search Unity

[c# hover car script] Change time till fullspeed

Discussion in 'Physics' started by Sneer1, Sep 9, 2016.

  1. Sneer1

    Sneer1

    Joined:
    Nov 10, 2014
    Posts:
    2
    Hello,
    I'm trying to change the time it takes until the car reaches maximum speed. Currently, it takes ~1 second until the car reaches maximum speed. I would like to change it to 2 or 3 seconds in the script. So the hover car should slowly gather some speed. I have tried to change the rigidbody drag, but it changes top speed only. The time it takes until it reaches top speed stays almost the same with any drag value. Here is my current hover script :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(Rigidbody))]
    5. public class HoverCarControl : MonoBehaviour
    6. {
    7.   Rigidbody m_body;
    8.   float m_deadZone = 0.1f;
    9.  
    10.   public float m_hoverForce = 9.0f;
    11.   public float m_hoverHeight = 2.0f;
    12.   public GameObject[] m_hoverPoints;
    13.  
    14.   public float m_forwardAcl = 100.0f;
    15.   public float m_backwardAcl = 25.0f;
    16.   float m_currThrust = 0.0f;
    17.  
    18.   public float m_turnStrength = 10f;
    19.   float m_currTurn = 0.0f;
    20.  
    21.   public GameObject m_leftAirBrake;
    22.   public GameObject m_rightAirBrake;
    23.  
    24.   int m_layerMask;
    25.  
    26.   void Start()
    27.   {
    28.     m_body = GetComponent<Rigidbody>();
    29.  
    30.     m_layerMask = 1 << LayerMask.NameToLayer("Characters");
    31.     m_layerMask = ~m_layerMask;
    32.   }
    33.  
    34.   void OnDrawGizmos()
    35.   {
    36.  
    37.     //  Hover Force
    38.     RaycastHit hit;
    39.     for (int i = 0; i < m_hoverPoints.Length; i++)
    40.     {
    41.       var hoverPoint = m_hoverPoints [i];
    42.       if (Physics.Raycast(hoverPoint.transform.position,
    43.                           -Vector3.up, out hit,
    44.                           m_hoverHeight,
    45.                           m_layerMask))
    46.       {
    47.         Gizmos.color = Color.blue;
    48.         Gizmos.DrawLine(hoverPoint.transform.position, hit.point);
    49.         Gizmos.DrawSphere(hit.point, 0.5f);
    50.       } else
    51.       {
    52.         Gizmos.color = Color.red;
    53.         Gizmos.DrawLine(hoverPoint.transform.position,
    54.                        hoverPoint.transform.position - Vector3.up * m_hoverHeight);
    55.       }
    56.     }
    57. }
    58.  
    59.   void Update()
    60.   {
    61.  
    62.     // Main Thrust
    63.     m_currThrust = 0.0f;
    64.     float aclAxis = Input.GetAxis("Vertical");
    65.     if (aclAxis > m_deadZone)
    66.       m_currThrust = aclAxis * m_forwardAcl;
    67.     else if (aclAxis < -m_deadZone)
    68.       m_currThrust = aclAxis * m_backwardAcl;
    69.  
    70.     // Turning
    71.     m_currTurn = 0.0f;
    72.     float turnAxis = Input.GetAxis("Horizontal");
    73.     if (Mathf.Abs(turnAxis) > m_deadZone)
    74.       m_currTurn = turnAxis;
    75.     }
    76.  
    77.   void FixedUpdate()
    78.   {
    79.  
    80.     //  Hover Force
    81.     RaycastHit hit;
    82.     for (int i = 0; i < m_hoverPoints.Length; i++)
    83.     {
    84.       var hoverPoint = m_hoverPoints [i];
    85.       if (Physics.Raycast(hoverPoint.transform.position,
    86.                           -Vector3.up, out hit,
    87.                           m_hoverHeight,
    88.                           m_layerMask))
    89.         m_body.AddForceAtPosition(Vector3.up
    90.           * m_hoverForce
    91.           * (1.0f - (hit.distance / m_hoverHeight)),
    92.                                   hoverPoint.transform.position);
    93.       else
    94.       {
    95.         if (transform.position.y > hoverPoint.transform.position.y)
    96.           m_body.AddForceAtPosition(
    97.             hoverPoint.transform.up * m_hoverForce,
    98.             hoverPoint.transform.position);
    99.         else
    100.           m_body.AddForceAtPosition(
    101.             hoverPoint.transform.up * -m_hoverForce,
    102.             hoverPoint.transform.position);
    103.       }
    104.     }
    105.  
    106.     // Forward
    107.     if (Mathf.Abs(m_currThrust) > 0)
    108.       m_body.AddForce(transform.forward * m_currThrust);
    109.  
    110.     // Turn
    111.     if (m_currTurn > 0)
    112.     {
    113.       m_body.AddRelativeTorque(Vector3.up * m_currTurn * m_turnStrength);
    114.     } else if (m_currTurn < 0)
    115.     {
    116.       m_body.AddRelativeTorque(Vector3.up * m_currTurn * m_turnStrength);
    117.     }
    118.     }
    119. }
    120.  
    Would be cool, if someone could point me into the right direction.
     
    Last edited: Sep 9, 2016
  2. CrymX

    CrymX

    Joined:
    Feb 16, 2015
    Posts:
    179
    Drop down the "m_forwardAcl" ?