Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by spraynpay · Mar 22, 2017 at 03:23 AM · triggercarboost

Below script is a car controller and i want the car to boost when hit a trigger object. Still new with unity T^ T

using System; using UnityEngine; using System.Collections;

namespace UnityStandardAssets.Vehicles.Car { internal enum CarDriveType { FrontWheelDrive, RearWheelDrive, FourWheelDrive }

 internal enum SpeedType
 {
     MPH,
     KPH
 }

 public class CarController : MonoBehaviour
 {
     private GameManagerScript GMS;
     
     [SerializeField] private CarDriveType m_CarDriveType = CarDriveType.FourWheelDrive;
     [SerializeField] private WheelCollider[] m_WheelColliders = new WheelCollider[4];
     [SerializeField] private GameObject[] m_WheelMeshes = new GameObject[4];
     [SerializeField] private Vector3 m_CentreOfMassOffset;
     [SerializeField] private float m_MaximumSteerAngle;
     [SerializeField] private float m_FullTorqueOverAllWheels;
     [SerializeField] private float m_ReverseTorque;
     [SerializeField] private float m_MaxHandbrakeTorque;
     [SerializeField] private float m_Downforce = 100f;
     [SerializeField] private SpeedType m_SpeedType;
     [SerializeField] private float m_Topspeed = 200;
     [SerializeField] private static int NoOfGears = 7;
     [SerializeField] private float m_RevRangeBoundary = 1f;
     [SerializeField] private float m_SlipLimit;
     [SerializeField] private float m_BrakeTorque;

     private Quaternion[] m_WheelMeshLocalRotations;
     private Vector3 m_Prevpos, m_Pos;
     private float m_SteerAngle;
     private int m_GearNum;
     private float m_GearFactor;
     private float m_OldRotation;
     private float m_CurrentTorque;
     private Rigidbody m_Rigidbody;
     private const float k_ReversingThreshold = 0.01f;

     public bool Skidding { get; private set; }
     public float BrakeInput { get; private set; }
     public float CurrentSteerAngle{ get { return m_SteerAngle; }}
     public float CurrentSpeed{ get { return m_Rigidbody.velocity.magnitude*2.23693629f; }}
     public float MaxSpeed{get { return m_Topspeed; }}
     public float Revs { get; private set; }
     public float AccelInput { get; private set; }

     // Use this for initialization
     private void Start()
     {
         GMS = GameObject.Find("GameManager").GetComponent<GameManagerScript>();

         m_WheelMeshLocalRotations = new Quaternion[4];
         for (int i = 0; i < 4; i++)
         {
             m_WheelMeshLocalRotations[i] = m_WheelMeshes[i].transform.localRotation;
         }
         m_WheelColliders[0].attachedRigidbody.centerOfMass = m_CentreOfMassOffset;

         m_MaxHandbrakeTorque = float.MaxValue;

         m_Rigidbody = GetComponent<Rigidbody>();
         m_CurrentTorque = m_FullTorqueOverAllWheels;
     }


     private void GearChanging()
     {
         float f = Mathf.Abs(CurrentSpeed/MaxSpeed);
         float upgearlimit = (1/(float) NoOfGears)*(m_GearNum + 1);
         float downgearlimit = (1/(float) NoOfGears)*m_GearNum;

         if (m_GearNum > 0 && f < downgearlimit)
         {
             m_GearNum--;
         }

         if (f > upgearlimit && (m_GearNum < (NoOfGears - 1)))
         {
             m_GearNum++;
         }
     }


     // simple function to add a curved bias towards 1 for a value in the 0-1 range
     private static float CurveFactor(float factor)
     {
         return 1 - (1 - factor)*(1 - factor);
     }


     // unclamped version of Lerp, to allow value to exceed the from-to range
     private static float ULerp(float from, float to, float value)
     {
         return (1.0f - value)*from + value*to;
     }


     private void CalculateGearFactor()
     {
         float f = (1/(float) NoOfGears);
         // gear factor is a normalised representation of the current speed within the current gear's range of speeds.
         // We smooth towards the 'target' gear factor, so that revs don't instantly snap up or down when changing gear.
         var targetGearFactor = Mathf.InverseLerp(f*m_GearNum, f*(m_GearNum + 1), Mathf.Abs(CurrentSpeed/MaxSpeed));
         m_GearFactor = Mathf.Lerp(m_GearFactor, targetGearFactor, Time.deltaTime*5f);
     }


     private void CalculateRevs()
     {
         // calculate engine revs (for display / sound)
         // (this is done in retrospect - revs are not used in force/power calculations)
         CalculateGearFactor();
         var gearNumFactor = m_GearNum/(float) NoOfGears;
         var revsRangeMin = ULerp(0f, m_RevRangeBoundary, CurveFactor(gearNumFactor));
         var revsRangeMax = ULerp(m_RevRangeBoundary, 1f, gearNumFactor);
         Revs = ULerp(revsRangeMin, revsRangeMax, m_GearFactor);
     }


     public void Move(float steering, float accel, float footbrake, float handbrake)
     {
         if (GMS.counterDownDone == true)
         {

             for (int i = 0; i < 4; i++)
             {
                 Quaternion quat;
                 Vector3 position;
                 m_WheelColliders[i].GetWorldPose(out position, out quat);
                 m_WheelMeshes[i].transform.position = position;
                 m_WheelMeshes[i].transform.rotation = quat;
             }

             //clamp input values
             steering = Mathf.Clamp(steering, -1, 1);
             AccelInput = accel = Mathf.Clamp(accel, 0, 1);
             BrakeInput = footbrake = -1 * Mathf.Clamp(footbrake, -1, 0);
             handbrake = Mathf.Clamp(handbrake, 0, 1);

             //Set the steer on the front wheels.
             //Assuming that wheels 0 and 1 are the front wheels.
             m_SteerAngle = steering * m_MaximumSteerAngle;
             m_WheelColliders[0].steerAngle = m_SteerAngle;
             m_WheelColliders[1].steerAngle = m_SteerAngle;
             ApplyDrive(accel, footbrake);
             CapSpeed();

             //Set the handbrake.
             //Assuming that wheels 2 and 3 are the rear wheels.
             if (handbrake > 0f)
             {
                 var hbTorque = handbrake * m_MaxHandbrakeTorque;
                 m_WheelColliders[2].brakeTorque = hbTorque;
                 m_WheelColliders[3].brakeTorque = hbTorque;
             }


             CalculateRevs();
             GearChanging();

             AddDownForce();
         }
     }


     private void CapSpeed()
     {
         
         

             float speed = m_Rigidbody.velocity.magnitude;
             switch (m_SpeedType)
             {
                 case SpeedType.MPH:

                     speed *= 2.23693629f;
                     if (speed > m_Topspeed)
                         m_Rigidbody.velocity = (m_Topspeed / 2.23693629f) * m_Rigidbody.velocity.normalized;
                     break;

                 case SpeedType.KPH:
                     speed *= 3.6f;
                     if (speed > m_Topspeed)
                         m_Rigidbody.velocity = (m_Topspeed / 3.6f) * m_Rigidbody.velocity.normalized;
                     break;
             }
         }
     


     private void ApplyDrive(float accel, float footbrake)
     {

         float thrustTorque;
         switch (m_CarDriveType)
         {
             case CarDriveType.FourWheelDrive:
                 thrustTorque = accel * (m_CurrentTorque / 4f);
                 for (int i = 0; i < 4; i++)
                 {
                     m_WheelColliders[i].motorTorque = thrustTorque;
                 }
                 break;

             case CarDriveType.FrontWheelDrive:
                 thrustTorque = accel * (m_CurrentTorque / 2f);
                 m_WheelColliders[0].motorTorque = m_WheelColliders[1].motorTorque = thrustTorque;
                 break;

             case CarDriveType.RearWheelDrive:
                 thrustTorque = accel * (m_CurrentTorque / 2f);
                 m_WheelColliders[2].motorTorque = m_WheelColliders[3].motorTorque = thrustTorque;
                 break;

         }

         for (int i = 0; i < 4; i++)
         {
             if (CurrentSpeed > 5 && Vector3.Angle(transform.forward, m_Rigidbody.velocity) < 50f)
             {
                 m_WheelColliders[i].brakeTorque = m_BrakeTorque*footbrake;
             }
             else if (footbrake > 0)
             {
                 m_WheelColliders[i].brakeTorque = 0f;
                 m_WheelColliders[i].motorTorque = -m_ReverseTorque*footbrake;
             }
         }
     }


     // this is used to add more grip in relation to speed
     private void AddDownForce()
     {
         for (int i = 0; i < 4; i++) {
             if (m_WheelColliders[i].isGrounded){
             m_WheelColliders [0].attachedRigidbody.AddForce (-transform.up * m_Downforce *
                 m_WheelColliders [0].attachedRigidbody.velocity.magnitude);
             }
             else
             {
                 if (m_WheelColliders[i].isGrounded){
                     m_WheelColliders [0].attachedRigidbody.AddForce (-transform.up * 0f *
                                                                      m_WheelColliders [0].attachedRigidbody.velocity.magnitude);
                 }
             }
         }
         }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Adding Speed to onTriggerEntered 1 Answer

How to increase the speed of an object's movement when it enters a trigger? 1 Answer

How to make audio play when passes a collider 2 Answers

Enter Car On KeyDown 1 Answer

How can I make a trigger detect Terrain? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges