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
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);
}
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
Follow this Question
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