Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by joseenriquejoson · Aug 24, 2021 at 08:36 AM · scripting problemspeedcar game

Car goes way beyond the speed limit even though the script already assigned the current speed just to reach the speed limit

Will somebody help me in my problem? I already prepared the car with Colliders, the Rigidbody, and I followed a tutorial (Link: https://youtu.be/Z4HA8zJhGEk - "Simple Car Controller in Unity Tutorial") for scripting the car. I also got the speed of the car in the script, determined the maximum speed of the car by searching it in Google, and added a few modifications to the script. Here now lies the problem: the car goes way beyond the maximum speed although the maximum speed is set and I already putted the codes that I found from https://answers.unity.com/questions/1040591/how-to-set-speed-limit-of-car.html and https://answers.unity.com/questions/1067097/car-speed-limit-unity5.html onto the script. What could I have done wrong in my script so that this will happen? Here is the code: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CarController2 : MonoBehaviour { private const string HORIZONTAL = "Horizontal"; private const string VERTICAL = "Vertical";

 public Rigidbody m_Rigidbody;
 public float torque;
 private Vector3 m_EulerAngleVelocity;

 private float horizontalInput;
 private float verticalInput;
 private float currentsteerAngle;
 private float currentbreakForce;
 private float timeCount;
 private bool isBreaking;

 [SerializeField] private float motorPower;
 [SerializeField] private float breakForce;
 [SerializeField] private float accelerationRate;
 [SerializeField] private float currentSpeed;
 [SerializeField] private float maxSpeed = 195;
 [SerializeField] private float maxSteerAngle;

 [SerializeField] private WheelCollider frontLeftWheelCollider;
 [SerializeField] private WheelCollider frontRightWheelCollider;
 [SerializeField] private WheelCollider rearLeftWheelCollider;
 [SerializeField] private WheelCollider rearRightWheelCollider;

 [SerializeField] private Transform frontLeftWheelTransform;
 [SerializeField] private Transform frontRightWheelTransform;
 [SerializeField] private Transform rearLeftWheelTransform;
 [SerializeField] private Transform rearRightWheelTransform;

 private void Start()
 {
     m_Rigidbody = GetComponent<Rigidbody>();
 }

 private void FixedUpdate()
 {
     GetInput();
     HandleMotor();
     HandleSteering();
     UpdateWheels();
     CarTurn();
     SpeedOfCar();
 }

 private void GetInput()
 {
     horizontalInput = Input.GetAxis(HORIZONTAL);
     verticalInput = Input.GetAxis(VERTICAL);
     isBreaking = Input.GetKey(KeyCode.Space);
     if (Input.GetKey(KeyCode.Space))
     {
         Debug.Log("You hitted the brakes.");
     }
 }

 private void HandleMotor()
 {
     rearLeftWheelCollider.motorTorque = verticalInput * motorPower;
     rearRightWheelCollider.motorTorque = verticalInput * motorPower;
     currentbreakForce = isBreaking ? breakForce : 0f;
     ApplyBreaking();
 }

 private void ApplyBreaking()
 {
     frontLeftWheelCollider.brakeTorque = currentbreakForce;
     frontRightWheelCollider.brakeTorque = currentbreakForce;
     rearLeftWheelCollider.brakeTorque = currentbreakForce;
     rearRightWheelCollider.brakeTorque = currentbreakForce;
 }

 private void HandleSteering()
 {
     currentsteerAngle = maxSteerAngle * horizontalInput;
     frontLeftWheelCollider.steerAngle = currentsteerAngle;
     frontRightWheelCollider.steerAngle = currentsteerAngle;
 }

 private void UpdateWheels()
 {
     UpdateSingleWheel(frontLeftWheelCollider, frontLeftWheelTransform);
     UpdateSingleWheel(frontRightWheelCollider, frontRightWheelTransform);
     UpdateSingleWheel(rearLeftWheelCollider, rearLeftWheelTransform);
     UpdateSingleWheel(rearRightWheelCollider, rearRightWheelTransform);
 }

 private void UpdateSingleWheel(WheelCollider wheelCollider, Transform wheelTransform)
 {
     Vector3 pos;
     Quaternion rot;
     wheelCollider.GetWorldPose(out pos, out rot);
     wheelTransform.rotation = rot;
     wheelTransform.position = pos;
 }

 private void CarTurn()
 {

     float turn = Input.GetAxis("Horizontal");
     m_Rigidbody.AddTorque(transform.up * torque * turn);
 }

 private void SpeedOfCar()
 {
     currentSpeed = m_Rigidbody.velocity.magnitude*3.6f;
     currentSpeed = Mathf.Round(currentSpeed);
     accelerationRate = maxSpeed/16.7f;
     accelerationRate = Mathf.Round(accelerationRate);

     if(currentSpeed <= 195){
         for(int i = 0; i < 4; i++) { rearLeftWheelCollider.motorTorque = verticalInput * motorPower; }
         for(int i = 0; i < 4; i++) { rearRightWheelCollider.motorTorque = verticalInput * motorPower; }
     }
     else { 
         for(int i = 0; i < 4; i++) { rearLeftWheelCollider.motorTorque = 0; }
         for(int i = 0; i < 4; i++) { rearRightWheelCollider.motorTorque = 0; }
     }
 }

}

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

263 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 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 avatar image

Related Questions

Help my car only turns right 0 Answers

C# Min/Max Rotation based on Float? 1 Answer

I can't get my car move forward with the given scripts. 1 Answer

How to immediately CHANGE SPEED from spotted position to another?) 1 Answer

Speed of the script 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