• 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 Chi0gy · Nov 29, 2017 at 10:51 AM · inputaxisracing gamexbox controller

Xbox one triggers don't work in my racing game.

Hi, I need help, I am trying to program the controller of Xbox One, especially the triggers (that will be the accelerator and reverse), and doesn't respond, press and don't respond, I tried to use 3rd axis by InputManager but it did not work, I downloaded the InControl plugin to see if it works, and it works but the triggers do not share the same -1, 0, 1 axis as 3rd axis, so I cant find another way to get the car to go forward or backward with the triggers having the individual axes, I would need them to be shared axes, can someone please help me, here is my script, this is the way I wrote but it is not giving any results:

 using UnityEngine;
 using System.Collections;
 using TMPro;
 using InControl;
 [RequireComponent(typeof(Rigidbody))]
 public class PlayerEngine : MonoBehaviour
 {
     // Variaveis do Torque e Velocidade do Veiculo.
     public string nomeDoCarro;
     public float torqueNormal = 1f;
     public float poderDoBoost = 1f;
     private float torque;
     public float velocidadeAtual;
     public float velocidadeMaxima = 300f;
     //public float desaceleracao = 5f;
     public float velocidadeMaximaDeRe = 100f;
     private float frequenciaDoMotor;
     public float brake = 500f;
 
     public Transform centroDaMassa;
     
     // Pneus
 
     public WheelCollider[] pneus = new WheelCollider[4];
     public Transform[] meshesDePneus = new Transform[4];
 
     private Rigidbody m_rigidbody;
 
     // Stunt
 
     public float virarZ = 250f;
     public float virarX = 250f;
 
     // Freios
 
     private bool handBrake = false;
     public float forcaDoFreioDeMao = 500f;
 
     // Embreagem
 
     public int[] numeroDeMarchas;
     public int marchas;
 
     // Velocidade do Volante.
 
     public float baixissimaVelocidadeDoVolante = 50f;
     public float velocidadeBaixa = 10f;
     public float velocidadeAlta = 1;
 
     // Fricção do Pneu.
     private float minhaFriccaoDoLado;
     private float minhaFriccaoDaFrente;
     public float derrapagemLateral = 0.001f;
     public float derrapagemFrontal = 0.70f;
 
 
     // efeito de colisão
     public GameObject spark;
     public GameObject somDeColisão;
 
     // Nitro
     private float temporizador;
     private bool boostAcionado = false; // Quando acionado o Nitro/Boost o boolean se tornará verdadeiro.(hehe meio obvio né?)
     public ParticleEmitter chamaDireita;
     public ParticleEmitter chamaEsquerda;
     public bool boostConcentrado = false;
     public ParticleEmitter chamaDireita2;
     public ParticleEmitter chamaEsquerda2;
     private int Boost;
     public int quantidadeDeBoost = 1000;
     public int limiteDoBoost = 1000;
     public GameObject SomDoNitro;
     public GameObject contadorDeNitro;
     public GameObject NitroAcionado;
     public GameObject NitroDesativado;
 
     // Turbo
     public GameObject SomDoTurbo;
     public GameObject SomDoTurbo2;
     public GameObject TurboAndSupercharger;
 
     // -- Valor Da Frequencia Do Motor
     public float frequencia = 0.8f;
 
     // Tração
     public bool quatroPorQuatro = false;
     public bool traçãoDianteira = false;
     public bool traçãoTraseira = false;
 
     // Controle/Joystick
     private InputDevice Joystick;
 
     void Start()
     {
         m_rigidbody = GetComponent<Rigidbody>();
         m_rigidbody.centerOfMass = centroDaMassa.localPosition;
         Boost = 1000;
         valoresDeFriccaoDoPneu ();
         torque = torqueNormal;
     }
 
     void valoresDeFriccaoDoPneu () {
         minhaFriccaoDaFrente = pneus [3].forwardFriction.stiffness;
         minhaFriccaoDoLado = pneus [3].sidewaysFriction.stiffness;
         minhaFriccaoDaFrente = pneus [2].forwardFriction.stiffness;
         minhaFriccaoDoLado = pneus [2].sidewaysFriction.stiffness;
     }
 
     void Update()
     {
         UpdateMeshesPositions();
         EngineSound();
 
         if(Joystick.DPadLeft)
             transform.Rotate(0, 0, virarZ*Time.deltaTime);
         if(Joystick.DPadRight)
             transform.Rotate(0, 0, -virarZ*Time.deltaTime);
         if (Input.GetButton("RollToTheLeft"))
             transform.Rotate(0, 0, virarZ*Time.deltaTime);
         if (Input.GetButton("RollToTheRight"))
             transform.Rotate(0, 0, -virarZ*Time.deltaTime);
 
     }
 
     void FixedUpdate ()
     {
         HandBrakes ();
 
 
         float direção = Input.GetAxis ("Steer");
         
 
         float anguloFinal = direção * 45f;
 
 
 
         bool limitador = false;
 
         pneus [0].steerAngle = anguloFinal;
         pneus [1].steerAngle = anguloFinal;      
 
         for (int i = 0; i < 4; i++) {
             {
                 velocidadeAtual = 2 * Mathf.PI * pneus [i].radius * pneus [i].rpm * 60 / 1000;
                 velocidadeAtual = Mathf.Round (velocidadeAtual);
                 velocidadeAtual = Mathf.Abs (velocidadeAtual);
                 torque = Mathf.Abs (torque);
                 FunçãoBoost ();
 
                 if (traçãoTraseira) {
 
                     if (velocidadeAtual < velocidadeMaxima && velocidadeAtual > -velocidadeMaximaDeRe) {
                         
                         float TriggerAxis = 0.0f;
                         TriggerAxis = Input.GetAxis ("Acc-Rev");
 
                         pneus [2].motorTorque = torque * TriggerAxis;
                         pneus [3].motorTorque = torque * TriggerAxis;
 
 
                         if(Joystick.RightTrigger){ // <= Here is the problem
                             TriggerAxis = 1.0f;
                             Mathf.Clamp (TriggerAxis, -1.0f, 1.0f);
                             print ("Value" + " " + TriggerAxis);
 
                         }
                         if(Joystick.LeftTrigger){  // <= Here is the problem
 
                             TriggerAxis = -1.0f;
                             Mathf.Clamp (TriggerAxis, -1.0f, 1.0f);
                             print ("Value" + " " + TriggerAxis);
                         }
 
                     } else {
                         limitador = true;
                         pneus [2].motorTorque = 0;
                         pneus [3].motorTorque = 0;
                     }
                     if (limitador) {
                         pneus [2].motorTorque = 0;
                         pneus [3].motorTorque = 0;
                     }
                 } else if (traçãoDianteira) {
 
                     if (velocidadeAtual < velocidadeMaxima && velocidadeAtual > -velocidadeMaximaDeRe) {
                         pneus [0].motorTorque = torque * Input.GetAxis ("Acc-Rev");
                         pneus [1].motorTorque = torque * Input.GetAxis ("Acc-Rev");
                     } else {
                         limitador = true;
                         pneus [0].motorTorque = 0;
                         pneus [1].motorTorque = 0;
                     }
                     if (limitador) {
                         pneus [0].motorTorque = 0;
                         pneus [1].motorTorque = 0;
                     }
                 } else if (quatroPorQuatro) {
 
                     if (velocidadeAtual < velocidadeMaxima && velocidadeAtual > -velocidadeMaximaDeRe) {
                         pneus [0].motorTorque = torque * Input.GetAxis ("Acc-Rev");
                         pneus [1].motorTorque = torque * Input.GetAxis ("Acc-Rev");
                         pneus [2].motorTorque = torque * Input.GetAxis ("Acc-Rev");
                         pneus [3].motorTorque = torque * Input.GetAxis ("Acc-Rev");
                     } else {
                         limitador = true;
                         pneus [0].motorTorque = 0;
                         pneus [1].motorTorque = 0;
                         pneus [2].motorTorque = 0;
                         pneus [3].motorTorque = 0;
                     }
                     if (limitador) {
                         pneus [0].motorTorque = 0;
                         pneus [1].motorTorque = 0;
                         pneus [2].motorTorque = 0;
                         pneus [3].motorTorque = 0;
                     }
                 }
             }
             // brake
             if (Input.GetButton ("Acc-Rev") == false) {
                 pneus [0].brakeTorque = brake;
                 pneus [1].brakeTorque = brake;
                 pneus [2].brakeTorque = brake;
                 pneus [3].brakeTorque = brake;
             } else {
                 pneus [0].brakeTorque = 0;
                 pneus [1].brakeTorque = 0;
                 pneus [2].brakeTorque = 0;
                 pneus [3].brakeTorque = 0;
             }
         }
 
         float fatorDeVelocidade = GetComponent<Rigidbody> ().velocity.magnitude / baixissimaVelocidadeDoVolante;
         float anguloAtualDoVolante = Mathf.Lerp (velocidadeBaixa, velocidadeAlta, fatorDeVelocidade);
         anguloAtualDoVolante *= Input.GetAxis ("Steer");
 
         pneus [0].steerAngle = anguloAtualDoVolante;
         pneus [1].steerAngle = anguloAtualDoVolante;
 }
 
     void UpdateMeshesPositions()
     {
         for (int i = 0; i < 4; i++)
         {
             Quaternion quat;
             Vector3 pos;
             pneus[i].GetWorldPose(out pos, out quat);
 
             meshesDePneus[i].position = pos;
             meshesDePneus[i].rotation = quat;
         }
     }
 
     void EngineSound() {
         for (int i = 0; i < numeroDeMarchas.Length; i++) {
             if (numeroDeMarchas [i] > velocidadeAtual) {
                 marchas = i;
                 break;
             }
         }
         float valorMaxDaMarcha = 0.00f;
         float valorMinDaMarcha = 0.00f;
 
         if (marchas == 0) {
             valorMinDaMarcha = 0;
         } else {
             valorMinDaMarcha = numeroDeMarchas [marchas - 1];
         }
         valorMaxDaMarcha = numeroDeMarchas [marchas];
         frequenciaDoMotor = ((velocidadeAtual - valorMinDaMarcha) / (valorMaxDaMarcha - valorMinDaMarcha)) + frequencia;
         GetComponent<AudioSource> ().pitch = frequenciaDoMotor;
         TurboAndSupercharger.GetComponent<AudioSource> ().pitch = frequenciaDoMotor;
 
         if (marchas == 0) {
             SomDoTurbo.SetActive (false);
             SomDoTurbo2.SetActive (true);
         }
 
         if (marchas == 2) {
             SomDoTurbo.SetActive (true);
             SomDoTurbo2.SetActive (false);
         }
         if (marchas == 4) {
             SomDoTurbo.SetActive (false);
             SomDoTurbo2.SetActive (true);
         } 
         if (marchas == 6) {
             SomDoTurbo.SetActive (true);
             SomDoTurbo2.SetActive (false);
         }
         if (marchas == 8) {
             SomDoTurbo.SetActive (false);
             SomDoTurbo2.SetActive (true);
         }
         if (marchas == 10) {
             SomDoTurbo.SetActive (true);
             SomDoTurbo2.SetActive (false);
         }
         if (marchas == 12) {
             SomDoTurbo.SetActive (false);
             SomDoTurbo2.SetActive (true);
         }
         if (marchas == 14) {
             SomDoTurbo.SetActive (true);
             SomDoTurbo2.SetActive (false);
         }
     }
 
     void HandBrakes (){
         if (Input.GetButton("E-Brake") || Joystick.Action2) {
             handBrake = true;
         } else {
             handBrake = false;
         }
 
         //Se for Verdadeiro, irá Aplicar o Freio de Mão.
         if (handBrake) {
             if (velocidadeAtual > 1) {
                 pneus [2].brakeTorque = forcaDoFreioDeMao;
                 pneus [3].brakeTorque = forcaDoFreioDeMao;
                 pneus [2].motorTorque = 0;
                 pneus [3].motorTorque = 0;
                 AplicarFreioDeMao (derrapagemFrontal, derrapagemLateral);
             } else if (velocidadeAtual < 0) {
                 pneus [2].brakeTorque = forcaDoFreioDeMao;
                 pneus [3].brakeTorque = forcaDoFreioDeMao;
                 pneus [2].motorTorque = 0;
                 pneus [3].motorTorque = 0;
                 AplicarFreioDeMao (1f, 2f);
             } else {
                 AplicarFreioDeMao (1f, 2f);
             }
         } else {
             pneus [0].brakeTorque = 0;
             pneus [1].brakeTorque = 0;
             AplicarFreioDeMao (minhaFriccaoDaFrente, minhaFriccaoDoLado);
         }
     }
 
     void AplicarFreioDeMao (float friccaoFrontalAtual, float friccaoLateralAtual) {
         WheelFrictionCurve tempPneusRL = pneus [2].forwardFriction;
         tempPneusRL.stiffness = friccaoFrontalAtual;
         pneus [2].forwardFriction = tempPneusRL;
 
         WheelFrictionCurve tempPneusRR = pneus [3].forwardFriction;
         tempPneusRR.stiffness = friccaoFrontalAtual;
         pneus [3].forwardFriction = tempPneusRR;
 
         WheelFrictionCurve tempPneusRL2 = pneus [2].sidewaysFriction;
         tempPneusRL2.stiffness = friccaoLateralAtual;
         pneus [2].sidewaysFriction = tempPneusRL2;
 
         WheelFrictionCurve tempPneusRR2 = pneus [3].sidewaysFriction;
         tempPneusRR2.stiffness = friccaoLateralAtual;
         pneus [3].sidewaysFriction = tempPneusRR2;
 
     }
 
     void FunçãoBoost(){
         // ("boost") é o turbo, sabe como é né dificuldade para passar o oponente(Hit the GAS!  ...or Boost)
         if (Input.GetButton ("Boost") && velocidadeAtual > 25) {
             boostAcionado = true;
             NitroAcionado.SetActive (true);
             NitroDesativado.SetActive (false);
         } else {
             boostAcionado = false;
             NitroAcionado.SetActive (false);
             NitroDesativado.SetActive (true);
         }
         // Aqui o Computador verá se o botão do nitro foi acionado, ver se velocidade está acima de 25 KM/h e se a quantidade nitro não acabou. 
         if (boostAcionado && velocidadeAtual > 25 && Boost > 0) {
             torque = poderDoBoost; // esse valor substitui o padrão 602 de torque.
             chamaDireita.emit = true; // ativa a chama direita.
             chamaEsquerda.emit = true; // ativa a chama esquerda.
             if (boostConcentrado) {
                 chamaEsquerda2.emit = true;
                 chamaDireita2.emit = true;
             } else {
                 chamaEsquerda2.emit = false;
                 chamaDireita2.emit = false;
             }
             Boost--; // quando presionado o nitro vai subtrair até acabar mas, não se preocupe.
             Instantiate(SomDoNitro); // Vai instanciar o som do nitro.
         } else {
             torque = torqueNormal; // quando desativado voltará o torque normal ou padrão.
             chamaDireita.emit = false; // desativa a chama direita.
             chamaEsquerda.emit = false; // desativa a chama esquerda.
             if (boostConcentrado) {
                 chamaEsquerda2.emit = false;
                 chamaDireita2.emit = false;
             }
         }
         if (Boost < quantidadeDeBoost && !Input.GetButton ("Boost") && velocidadeAtual > 25) {
             Boost++; // aqui a que a festa do nitro continua ele recarrega aqui...
         }else if(Boost > quantidadeDeBoost){
             Boost = limiteDoBoost; // ...Porém não passar de 1000 (dividindo por 10) 100%.
         }
         // Vai mostrar quanto de nitro na tela tem em porcentagem (%).
         contadorDeNitro.GetComponentInChildren<TextMeshProUGUI> ().text = "Boost : " + (Boost / 10) + "%";
 
     }
 
     void OnCollisionEnter (Collision other){
         if(other.transform != transform){ // basicamente o if vai checar as faiscas produzidas pelo colisor a um objeto qualquer, se for o colisor colidir a ele mesmo, não retorna...
             for (int i = 0; i < other.contacts.Length; i++){  // resultado.
                 Instantiate(spark,other.contacts[i].point,Quaternion.identity);
                 Instantiate (somDeColisão, other.contacts [i].point, Quaternion.identity);
             }                              
         }
     }
 
     private void Awake(){
         Joystick = InputManager.ActiveDevice;
     }
 }

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

131 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

Related Questions

Problem with input axis "Submit" 1 Answer

how to rotate spirte towards the mouse or by the xbox controller at the same time 0 Answers

Google Cardboard Look Axis are the Opposite. 0 Answers

Argument: Input Axis Horizontal is not set up. 2 Answers

Joystick 3rd Axis problem on Linux 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges