Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 benedictwongxr · Jul 09, 2020 at 06:11 PM · wheelcollidercar physics

My vehicle keep on reversing none stop even when i stop pressing S key with wheel collider.

hi i am new to unity and i have no idea how there code causes my vehicle in the game to non stop reversing when i press S key. is not entirely non stop, but it reverse for a very long time only stop. enter code here

 /*    
  * This Script Controls the car!
  */
 using System.Collections;
 using System;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class LPPV_CarController : MonoBehaviour
 {
 
 
     public enum WheelType
     {
         FrontLeft, FrontRight, RearLeft, RearRight
     }
     public enum SpeedUnit
     {
         Imperial, Metric
     }
     [Serializable]
     class Wheel
     {
         public WheelCollider collider;
         public GameObject mesh;
         public WheelType wheelType;
     }
 
     [SerializeField] private Wheel[] wheels = new Wheel[4];
     [SerializeField] private float maxTorque = 2000f, maxBrakeTorque = 500f, maxSteerAngle = 30f; //max Torque of Wheels, max Brake Torque and Maximum Steer Angle of steerable wheels
     [SerializeField] private static int NoOfGears = 5;
     [SerializeField] private float downForce = 100f; // The Force to apply downwards so that car stays on track!
     [SerializeField] private SpeedUnit speedUnit;   //Speed Unit - Imperial - Miles Per Hour, Metric - Kilometers Per Hour
     [SerializeField] private float topSpeed = 140;
     [SerializeField] private Transform centerOfMass;
     [SerializeField] private Text speedText;
 
 #if UNITY_ANDROID || UNITY_IOS
     [SerializeField] private LPPV_VButton accelerateButton, brakeButton, handBrakeButton;
 #endif
 
     [HideInInspector] public bool Accelerating = false, Deccelerating = false, HandBrake = false;
     private Rigidbody _rgbd;
     public float CurrentSpeed
     {
         get
         {
             float speed = _rgbd.velocity.magnitude;
             if (speedUnit == SpeedUnit.Imperial)
                 speed *= 2.23693629f;
             else
                 speed *= 3.6f;
             return speed;
         }
     }
 
     private void VisualizeWheel(Wheel wheel)
     {
         //This method copies the position and rotation of the wheel collider and pastes it to the corresponding mesh!
         if (wheel.mesh == null || wheel.collider == null)
             return;
 
         Vector3 position;
         Quaternion rotation;
         wheel.collider.GetWorldPose(out position, out rotation);    //Fetch the position and Rotation from the WheelCollider into temporary variables
 
         wheel.mesh.transform.position = position;
         wheel.mesh.transform.rotation = rotation;
     }
 
     private void Start()
     {
         _rgbd = GetComponent<Rigidbody>();
         if (centerOfMass != null && _rgbd != null)
             _rgbd.centerOfMass = centerOfMass.localPosition;
         for (int i = 0; i < wheels.Length; ++i)
             VisualizeWheel(wheels[i]);
     }
 
     private void Move(float motorInput, float steerInput, bool handBrake)
     {
         HandBrake = handBrake;
         motorInput = Mathf.Clamp(motorInput, -1f, 1f);
         if (motorInput > 0f)
         {
             //Accelerate vehicle!
             Accelerating = true;
             Deccelerating = false;
         }
         else if (motorInput < 0f)
         {
             //Brake Vehicle!
             Accelerating = false;
             Deccelerating = true;
         }
 
         steerInput = Mathf.Clamp(steerInput, -1f, 1f);
         float steer = steerInput * maxSteerAngle;
 
         for (int i = 0; i < wheels.Length; ++i)
         {
             if (wheels[i].collider == null)
                 break;
             if (wheels[i].wheelType == WheelType.FrontLeft || wheels[i].wheelType == WheelType.FrontRight)
             {
                 wheels[i].collider.steerAngle = steer;
 
             }
             if (!handBrake)
             {
                 wheels[i].collider.brakeTorque = 0f;
                 if (Accelerating)
                     wheels[i].collider.motorTorque = motorInput * maxTorque / 4f;
                 else if (Deccelerating)
                     wheels[i].collider.motorTorque = motorInput * maxBrakeTorque / 4f;
             }
             else
             {
                 if (wheels[i].wheelType == WheelType.RearLeft || wheels[i].wheelType == WheelType.RearRight)
                     wheels[i].collider.brakeTorque = maxBrakeTorque * 20f;
                 wheels[i].collider.motorTorque = 0f;
             }
             if (wheels[i].mesh != null)
                 VisualizeWheel(wheels[i]);
         }
 
         StickToTheGround();
         ManageSpeed();
     }
 
     //This Method Applies down force so that car grips the ground strongly
     private void StickToTheGround()
     {
         if (wheels[0].collider == null)
             return;
         wheels[0].collider.attachedRigidbody.AddForce(-transform.up * downForce * wheels[0].collider.attachedRigidbody.velocity.magnitude);
     }
 
 
     //used to keep speed withing Minimum and maximum Speed Limits
     private void ManageSpeed()
     {
         float speed = _rgbd.velocity.magnitude;
         switch (speedUnit)
         {
             case SpeedUnit.Imperial:
                 speed *= 2.23693629f;
                 if (speed > topSpeed)
                     _rgbd.velocity = (topSpeed / 2.23693629f) * _rgbd.velocity.normalized;
                 break;
 
             case SpeedUnit.Metric:
                 speed *= 3.6f;
                 if (speed > topSpeed)
                     _rgbd.velocity = (topSpeed / 3.6f) * _rgbd.velocity.normalized;
                 break;
         }
     }
 
     private string imp = " MPH", met = " KPH";
     private void Update()
     {
         if (speedText != null)
         {
             if (speedUnit == SpeedUnit.Imperial)
                 speedText.text = ((int)CurrentSpeed).ToString() + imp;
             else
                 speedText.text = ((int)CurrentSpeed).ToString() + met;
         }
     }
     private void FixedUpdate()
     {
         float motor = 0f, steering = 0f;
         bool handBrakeInput = false;
 #if UNITY_STANDALONE || UNITY_WEBGL
             motor = Input.GetAxis("Vertical");
             steering = Input.GetAxis("Horizontal");
             handBrakeInput = Input.GetButton ("Jump");
 #endif
 
 #if UNITY_ANDROID || UNITY_IOS
         steering = Input.acceleration.x;
         if(accelerateButton != null)
         {
             if(accelerateButton.value)
                 motor = 1f;
         }
         if(brakeButton != null)
         {
             if(brakeButton.value)
                 motor = -1f;
         }
         if(handBrakeButton != null)
         {
             handBrakeInput = handBrakeButton.value;
             if(handBrakeButton.value)
             {
                 motor = 0f;
             }
         }
 #endif
         Move(motor, steering, handBrakeInput);
     }
 
 }
 




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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by HappyPixel27 · Jul 09, 2020 at 06:21 PM

That is because unity is calculating the speed at which your car is going and keeps your car moving like a real car would when its a a fast speed.

Comment
Add comment · Show 2 · Share
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
avatar image benedictwongxr · Jul 10, 2020 at 02:18 PM 0
Share

but even when i stop pressing the S key the car still reversing

avatar image HappyPixel27 benedictwongxr · Jul 10, 2020 at 05:25 PM 0
Share

Can you show us a video clip of this happening?

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

131 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Question! Why does the 'WheelCollider' Component act funny? 1 Answer

My car spins back?? Help! 0 Answers

How would you make a volumetric wheel collider? 1 Answer

How to properly use WheelCollider GetWorldPos 0 Answers

Unity Car Controller Collision Reverse 1 Answer


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