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 /
  • Help Room /
avatar image
0
Question by ElPatoRonaldo · Jan 21, 2017 at 08:24 PM · c#carcar gamestopping

How to fix this? C#

Can someone try to fix the problem? Well when I press the space button my car brakes and starts to move reverse without control. Thank you for your time and consideration!

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class Control2 : MonoBehaviour
 {
 
     public Text TxtSpeed;
     public WheelCollider front_left;
     public WheelCollider front_right;
     public WheelCollider back_left;
     public WheelCollider back_right;
     public Transform TD;
     public Transform TI;
     public Transform FD;
     public Transform FI;
     public float Torque;
     public float Speed;
     public float MaxSpeed = 200f;
     public int Brake = 10000;
     public float CoefAcceleration = 10f;
     public float WheelAngleMax = 10f;
     public float DAmax = 40f;
     public bool Freinage = false;
     public GameObject BackLight;
 
     void Start()
     {
         //Regla del centro de masa
         GetComponent<Rigidbody>().centerOfMass = new Vector3(0f, -0.9f, 0.2f);
     }
 
     void Update() {
      
         //Sonido Motor
         float Val_pitch= Speed / MaxSpeed + 1f;
         GetComponent<AudioSource>().pitch = Mathf.Clamp( Val_pitch, 1f, 3f); 
 
         //
         Speed = GetComponent<Rigidbody>().velocity.magnitude * 3.6f;
         TxtSpeed.text = "KM/H : " + (int)Speed;
         
         
         //Aceleración
         if (Input.GetKey(KeyCode.UpArrow) && Speed < MaxSpeed)
         {
             if(!Freinage)
             {
             front_left.brakeTorque = 0;
             front_right.brakeTorque = 0;
             back_left.brakeTorque = 0;
             back_right.brakeTorque = 0;
             back_left.motorTorque = Input.GetAxis("Vertical") * Torque * CoefAcceleration * Time.deltaTime;
             back_right.motorTorque = Input.GetAxis("Vertical") * Torque * CoefAcceleration * Time.deltaTime;
             }
             
         }
 
         //Desaceleración
         if (!Input.GetKey(KeyCode.UpArrow)  || Speed>MaxSpeed)
         {
             if (GetComponent<Rigidbody>().velocity.y > 0)
             {
                 back_left.motorTorque = -1000;
                 back_right.motorTorque = -1000;
             }
             else
             {
                 back_left.brakeTorque = 5000;
                 back_right.brakeTorque = 5000;
             }
               
         }
 
         //Dirección coche
         float DA = (((WheelAngleMax - DAmax) / MaxSpeed)* Speed) + DAmax;
         Debug.Log(DA);
         back_left.steerAngle = Input.GetAxis("Horizontal") * DA;
         back_right.steerAngle = Input.GetAxis("Horizontal") * DA;
 
         //Rotacion de ruedas
         TD.Rotate(back_left.rpm / 60 * 360 * Time.deltaTime, 0, 0);
         TI.Rotate(back_right.rpm / 60 * 360 * Time.deltaTime, 0, 0);
         FD.Rotate(front_right.rpm / 60 * 360 * Time.deltaTime, 0, 0);
         FI.Rotate(front_left.rpm / 60 * 360 * Time.deltaTime, 0, 0);
         //SteerAngle Mesh Ruedas
         FD.localEulerAngles = new Vector3(FD.localEulerAngles.x, back_left.steerAngle - FD.localEulerAngles.z, FD.localEulerAngles.z);
         FI.localEulerAngles = new Vector3(FI.localEulerAngles.x, back_right.steerAngle - FI.localEulerAngles.z, FI.localEulerAngles.z);
 
         //Frenado
        if(Input.GetKey(KeyCode.Space))
         {
             Freinage = true;
             BackLight.SetActive(true);
             back_left.brakeTorque = Mathf.Infinity;
             back_right.brakeTorque = Mathf.Infinity;
             front_left.brakeTorque = Mathf.Infinity;
             front_right.brakeTorque = Mathf.Infinity;
             back_left.motorTorque = 0;
             back_right.motorTorque = 0;
         }
        else
         {
             Freinage = false;
             BackLight.SetActive(false);
         }
         //Marcha Atras
         if (Input.GetKey(KeyCode.DownArrow))
         {
             front_left.brakeTorque = 0;
             front_right.brakeTorque = 0;
             back_left.brakeTorque = 0;
             back_right.brakeTorque = 0;
             back_left.motorTorque = Input.GetAxis("Vertical") * Torque * CoefAcceleration * Time.deltaTime;
             back_right.motorTorque = Input.GetAxis("Vertical") * Torque * CoefAcceleration * Time.deltaTime;
         }
     }
 }
 
     

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 SirPizza13104 · Jan 22, 2017 at 02:19 PM

At the point:

 if(Input.GetKey(KeyCode.Space))
          {
              Freinage = true;
              BackLight.SetActive(true);
              back_left.brakeTorque = Mathf.Infinity;
              back_right.brakeTorque = Mathf.Infinity;
              front_left.brakeTorque = Mathf.Infinity;
              front_right.brakeTorque = Mathf.Infinity;
              back_left.motorTorque = 0;
              back_right.motorTorque = 0;
          }
         else
          {
              Freinage = false;
              BackLight.SetActive(false);
          }

Try changing 'else' to 'if (Input.GetKeyUp(KeyCode.Space))'. Like this:

 if (Input.GetKeyUp(KeyCode.Space))
           {
               Freinage = false;
               BackLight.SetActive(false);
           }
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 ElPatoRonaldo · Jan 23, 2017 at 12:20 PM 0
Share

So close, I tried to do that you said with a little modification with (!) and changing the valor of brakeTorque $$anonymous$$athf.Infinity to a big number. Thanks you!

 if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
         {
             Freinage = true;
             BackLight.SetActive(true);
             back_left.brakeTorque = 90000;
             back_right.brakeTorque = 90000;
             front_left.brakeTorque = 90000;
             front_right.brakeTorque = 90000;
             back_left.motorTorque = 0;
             back_right.motorTorque = 0;
         }
         if (!Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
         {
             Freinage = false;
             BackLight.SetActive(false);`
 
 
 
 
 

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

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

simple car drift system 0 Answers

changing rpm when shifting 1 Answer

Problem in steering with wheelcolliders 0 Answers

unity 5 car tutorial 3D 1 Answer

Vector3 magnitude broken? 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