Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 /
This question was closed Aug 10, 2015 at 12:15 PM by galaboy for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by galaboy · Aug 06, 2015 at 11:43 AM · physicstransformvehicle

vehicle not accelerating after braking

hi, am learning the movement of vehicles using wheel colliders. i wrote a small script to move a car and when i apply brake to it the car stops and doesn't accelerate anymore. need help on how to once again accelerate.

 using UnityEngine;

using System.Collections;

public class new_car : MonoBehaviour {

 public float maxTorque = 50.0f;
 public float minTorque = 50.0f;
 //public float maxSteer;

 public WheelCollider[] wheelColliders = new WheelCollider[4];
 public Transform[] wheels = new Transform[4];

 public Transform centerOfMass;

 private Rigidbody n_rigidBody;
 //bool brakeApplied = false;

 // Use this for initialization
 void Start () 
 {
     n_rigidBody = GetComponent<Rigidbody>();
     n_rigidBody.centerOfMass = centerOfMass.localPosition;
 }
 
 // Update is called once per frame
 void Update () 
 {
     UpdateMeshes ();
 }

 void FixedUpdate()
 {
     float steer = Input.GetAxis ("Horizontal");
     float accelerate = Input.GetAxis ("Vertical");

     float maxSteer = steer * 30.0f;
     wheelColliders [0].steerAngle = maxSteer;
     wheelColliders [1].steerAngle = maxSteer;

     if (Input.GetKeyDown (KeyCode.UpArrow) || Input.GetKeyDown (KeyCode.DownArrow)) 
     {
         for(int i = 0; i < 4; i++)
         {
             wheelColliders[i].motorTorque = accelerate * maxTorque;
         }
     }
         if(Input.GetKeyDown(KeyCode.Space))
     {
         for(int i = 0; i < 4; i++)
         {
             wheelColliders[i].brakeTorque =  minTorque;
         }
     }
         
 }

 void UpdateMeshes()
 {
     for(int i = 0; i < 4; i++)
     {
         Quaternion quat;
         Vector3 pos;
         
         wheelColliders[i].GetWorldPose(out pos,out quat);
         
         wheels[i].position = pos;
         wheels[i].rotation = quat;
     }
 }

}

Comment
Add comment · Show 1
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 xSmurfKasketter · Aug 06, 2015 at 12:29 PM 0
Share

I have no experience in car scripting but from what i can see you set the brake torque however when you are done braking the brakiøe torque is still set to that value. Try setting the torque back when you are done braking. Same with acceleration

2 Replies

  • Sort: 
avatar image
1

Answer by galaboy · Aug 10, 2015 at 12:14 PM

thanks for the guys for trying to help me. i figured out the answer. below is the answer i was expecting. once again thanks for the unity community.

 using UnityEngine;
 using System.Collections;
 
     public class Car : MonoBehaviour
     {
         public float maxTorque = 50f;
         public float brakeTorque = 50f;
     
         public Transform centerOfMass;
     
         public WheelCollider[] wheelColliders = new WheelCollider[4];
         public Transform[] tireMeshes = new Transform[4];
     
         private Rigidbody m_rigidBody;
     
         void Start()
         {
             m_rigidBody = GetComponent<Rigidbody>();
             m_rigidBody.centerOfMass = centerOfMass.localPosition;
         }
     
         void Update()
         {
             UpdateMeshesPositions();
         }
     
         void FixedUpdate()
         {
             float steer = Input.GetAxis("Horizontal");
             float accelerate = Input.GetAxis("Vertical");
     
             float finalAngle = steer * 3.0f;
             wheelColliders[0].steerAngle = finalAngle;
             wheelColliders[1].steerAngle = finalAngle;
     
             for (int i = 1; i < 4; i++) 
             {
                 wheelColliders[i].motorTorque = accelerate * maxTorque;
                 //wheelColliders[3].motorTorque = accelerate * maxTorque;
             }       
     
             if (Input.GetKeyDown (KeyCode.Space)) 
             {
                 for(int i = 0; i < 4; i++)
                 {
                     wheelColliders[i].brakeTorque = brakeTorque;
                 }
             }
             
             if (Input.GetKeyUp (KeyCode.Space)) 
             {            
                 for(int i = 0; i < 4; i++)
                 {
                     wheelColliders[i].brakeTorque = 0.0f;
                 }
             }
         }
     
         void UpdateMeshesPositions()
         {
             for(int i = 0; i < 4; i++)
             {
                 Quaternion quat;
                 Vector3 pos;
                 wheelColliders[i].GetWorldPose(out pos, out quat);
     
                 tireMeshes[i].position = pos;
                 tireMeshes[i].rotation = quat;
             }
         }
     }

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 marmarart · Dec 04, 2016 at 09:21 PM 0
Share

Thanks for solution! :) I found this problem to and you helped me a lot.....

avatar image
0

Answer by imilanspinka · Aug 06, 2015 at 03:31 PM

I don't know, but

     if (Input.GetKeyDown (KeyCode.Space)) {
          for (int i = 0; i < 4; i++)
          {
              wheelColliders [i].brakeTorque = maxTorque - 40.0f;
          } 
          else
              for(int i = 0; i < 4; i++)
          {
              wheelColliders [i].brakeTorque = 0;
              wheelColliders [i].motorTorque = accelerate * maxTorque;
          }
     }

?

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

Follow this Question

Answers Answers and Comments

26 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

Related Questions

Does Rigidbody.freezRotation allow transform.Rotate ? 1 Answer

Applying force, rotation, physics and stable movement to objects. 0 Answers

Biomechanical model for motion capture 0 Answers

How do I make a first person player get in a vehicle? 0 Answers

How to rotate a rigidbody relative to another? 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