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
4
Question by book · Feb 08, 2013 at 11:37 AM · carwheel collidersmotortorque

How to Set a top speed on a car

I'm using motorTorque attached to the two front wheel colliders to power my car. But the car just seems to get faster and faster. How do I simply set a top speed so the car won't go over that speed.

Thanks in advance.

I just have a simple bit of code

void Update () {

     FrontLeftWheel.motorTorque = Input.GetAxis("Vertical") * speed;
     FrontRightWheel.motorTorque = Input.GetAxis("Vertical") * speed;
         
     FrontLeftWheel.brakeTorque = 0;
     FrontRightWheel.brakeTorque = 0;
     
     if (Input.GetButton("Jump"))
     {
         FrontLeftWheel.brakeTorque = 20;
         FrontRightWheel.brakeTorque = 20;
     }
     
     FrontLeftWheel.steerAngle = steer * Input.GetAxis("Horizontal");
     
     FrontRightWheel.steerAngle = steer * Input.GetAxis("Horizontal");
 
 }
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

6 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SubatomicHero · Apr 02, 2013 at 03:49 PM

Read up on the Mathf class, particularly the Clamp method.

Comment
Add comment · 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
1

Answer by LyanApps · Apr 02, 2013 at 12:49 PM

Not sure why this question was voted negative one as it seems like a legit problem. I would try:

 FixedUpdate(){
       //Find the speed by the square root of the velocity of the rigidbody of your car
       speed = rigidbody.velocity.sqrMagnitude;
       
       //Only add motorTorque if your speed is less then max speed
       if(speed < max_speed) {
         FrontLeftWheel.motorTorque = Input.GetAxis("Vertical") * motor_power;
         FrontRightWheel.motorTorque = Input.GetAxis("Vertical") * motor_power;
       }
 }
Comment
Add comment · Show 1 · 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 LyanApps · Apr 03, 2013 at 01:13 PM 0
Share

The Car.js in the tutorial at: http://u3d.as/content/unity-technologies/car-tutorial/1qU Computes a (drag) force that is inverse to the velocity that grows as the speed of the car approaches top_speed, and then this force is added to the rigid body to limit it's motion.

avatar image
0

Answer by Ruben_Chris · Dec 08, 2013 at 12:28 PM

This script worked for me. Hope it works for you! :) You can see in the console that it stays around 100 (the maxspeed i have set).

SCRIPT:

var WheelFR : WheelCollider; var WheelFL : WheelCollider; var WheelRR : WheelCollider; var WheelRL : WheelCollider;

var Speed = 10; var MaxSpeed = 100; var Breaking = 20; var Turning = 20;

private var CurrentSpeed;

function Update () { CurrentSpeed = rigidbody.velocity.sqrMagnitude; Debug.Log(CurrentSpeed + " | " + MaxSpeed);

 if (CurrentSpeed < MaxSpeed) {
     WheelRR.motorTorque = Input.GetAxis("Vertical") * Speed;
     WheelRL.motorTorque = Input.GetAxis("Vertical") * Speed;
 } else {
     WheelRR.motorTorque = 0;
     WheelRL.motorTorque = 0;
 }
 
 WheelRL.brakeTorque = 0;
 WheelRR.brakeTorque = 0;
 
 WheelFL.steerAngle = Input.GetAxis("Horizontal") * Turning;
 WheelFR.steerAngle = Input.GetAxis("Horizontal") * Turning;
 
 if (Input.GetButton("Jump")) {
     WheelRL.brakeTorque = Breaking;
     WheelRR.brakeTorque = Breaking;
 }

}

Comment
Add comment · 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
0

Answer by kannan21 · Feb 08, 2013 at 02:37 PM

Apply this code to your car.. using UnityEngine; using System.Collections;

public class TankManager : MonoBehaviour { public Transform tank; public float maxSpeed = 20; public float tankVelocity = 0;

Comment
Add comment · 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
0

Answer by kannan21 · Feb 08, 2013 at 02:37 PM

using UnityEngine;

using System.Collections;

public class CarMovement : MonoBehaviour

{

//car movement

public float maxSpeed = 20;

public float carSpeed = 0;

public float accelaration = 9;

public Update()

{

transform.Translate(new Vector3(0,0,carSpeed * Time.deltaTime));

move();

}

void Move()

{

if(Input.GetKey(KeyCode.W))

{

if(carSpeed <= maxSpeed)

carSpeed += accelaration * Time.deltaTime;

}

}

}

Comment
Add comment · Show 1 · 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 meat5000 ♦ · Dec 08, 2013 at 12:19 PM 1
Share

Damn dude! Format your code :D

  • 1
  • 2
  • ›

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

16 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

Related Questions

Unity 5 wheel collider throttle "stuck". 1 Answer

Applying Torque to wheelcollider 1 Answer

How to stop a car from veering in a particular direction? 0 Answers

The car flies away when wheel collider touch walls or other objects 1 Answer

Add Force or Motor Torque .. Which is better for creating a car? 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