Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Touchscreen Steering Wheel Rotation Example (Mouse Supported)

Discussion in 'Community Learning & Teaching' started by yasirkula, Aug 21, 2013.

  1. Farhan-Aqeel

    Farhan-Aqeel

    Joined:
    Aug 18, 2014
    Posts:
    2
    Updated in 2015.. Still a Life saver in 2018!!! Thankyou Maestro!!
     
  2. saadali211

    saadali211

    Joined:
    Feb 17, 2017
    Posts:
    19
    ✅ [ SOLUTION ] Here is a detailed video tutorial with free Asset follow this video to implement the Steering wheel script to a car.

     

    Attached Files:

    yasirkula likes this.
  3. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,783
    That's amazing! Thanks for creating this tutorial!
     
    saadali211 likes this.
  4. saadali211

    saadali211

    Joined:
    Feb 17, 2017
    Posts:
    19
    welcome
     
  5. RealityStudio

    RealityStudio

    Joined:
    Jul 25, 2012
    Posts:
    9

    Attached Files:

    yasirkula likes this.
  6. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,783
    Glad to see it in production!
     
  7. stf940

    stf940

    Joined:
    Jan 29, 2019
    Posts:
    4
    somebody explain me please! i bought a pickup truck from assets store. i have keyboard controls and mouse for freelock camera. how i can put steering wheel controls for mobile? im stuck at this please help i searched and tried all i saw
     
  8. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,783
    Attach the SteeringWheel script to the steering wheel UI object and use its GetClampedValue function to retrieve its input value. Alternatively, you can take a look at SimpleInput (it has a built-in steering wheel): https://github.com/yasirkula/UnitySimpleInput/
     
  9. parigojariya02

    parigojariya02

    Joined:
    Mar 2, 2019
    Posts:
    5
    Hello sir...
    I'm totally new in unity.
    I have done steering wheel as you suggest.
    Now what should i do so that my car follow the input of steeringwheel.
    I'm makign racing game for mobile.
    Please help me.
     
  10. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,783
    You should store SteeringWheel in a public variable and replace Input.GetAxis("Horizontal") with steeringWheelObject.GetClampedValue() in your script.
     
    parigojariya02 likes this.
  11. parigojariya02

    parigojariya02

    Joined:
    Mar 2, 2019
    Posts:
    5
    Thanks a lot sir for your quick reply.
     
  12. parigojariya02

    parigojariya02

    Joined:
    Mar 2, 2019
    Posts:
    5
    Thanks sir..,
    But,I actually didn't get where to I should store SteeringWheel as a public variable.I have tried "steeringWheelObject.GetClampedValue()" in my script,But I get error for "GetClampedValue()".
    Please Help me.
    Thanks.
     
  13. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,783
    public SteeringWheel steeringWheelObject;
    in your car controller/input script. Then, assign its value from the Inspector.
     
  14. parigojariya02

    parigojariya02

    Joined:
    Mar 2, 2019
    Posts:
    5
    Thanks sir..!
     
  15. nextage575

    nextage575

    Joined:
    Nov 4, 2019
    Posts:
    20
    hi
    i am trying to move and rotate my car with joystick , now i am i the phase of moving wheels according to joystick but i dont know how can i do that Please help me out.
    here is my code for car rotation and movement


    float moveHorizontal = CrossPlatformInputManager.GetAxis("Horizontal");
    float moveVertical = CrossPlatformInputManager.GetAxis("Vertical");
    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

    transform.Translate(movement * Time.deltaTime * carSpeed, Space.World);
    transform.LookAt(transform.position + movement);
     
  16. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,783
    There are three possible ways, actually:

    1) Add
    UnityStandardAssets.CrossPlatformInput.CrossPlatformInputManager.SetAxis("Horizontal", GetClampedValue());
    to the Update function of the SteeringWheel script (not tested, I haven't used CrossPlatformInput myself)
    2) Check out SimpleInput: https://assetstore.unity.com/packages/tools/input-management/simple-input-system-113033
    3) Create a "public SteeringWheel steeringWheel;" variable and assign the SteeringWheel object to that variable via Inspector. Then, make the following change to your code:
    float moveHorizontal = steeringWheel.GetClampedValue();
     
  17. vfxjex

    vfxjex

    Joined:
    Jun 3, 2013
    Posts:
    93
    You're a life saver... Thank you so very much!
    The code is perfectly implemented.
     
    yasirkula likes this.
  18. mateusschw

    mateusschw

    Joined:
    Aug 27, 2020
    Posts:
    7
    Olá cara, estou tendo um problema com o volante do meu carro, li todas as mensagens aqui, porém ainda não consigo implementar no meu projeto, tentei de várias maneiras usar o Simple Entrada, sem sucesso. Eu tenho dois scripts o primeiro é seu, porém, não sei como usar o "Get", como getAngle, e GetClampedValue. Meu segundo é mais simples. Os dois scripts funcionam em parte, o primeiro eu posso girar o volante, só as rodas não, o segundo gira com as rodas, mas não consigo girar a cola ao clicar com o mouse. Onde obtive este segundo script, dizia que era apenas uma questão de alterar a entrada "horizontal" por um toque. Não pude, se você pudesse dar uma olhada eu ficaria muito grato!

    [code = CSharp] usando UnityEngine;
    using System.Collections;

    public class Volante: MonoBehaviour {

    public enum Tipos {GirarEmX, GirarEmY, GirarEmZ};
    public GameObject objVolante;
    public Tipos Rotacao = Tipos.GirarEmZ;

    [Range (0.4f, 4.0f)] public float velGiroVolante = 2.0f;
    public bool inverterGiro = false;

    float rotacInicVolantAxis, angulo2Volante;

    void Start () {
    if (objVolante) {
    switch (Rotacao) {
    case Tipos.GirarEmX:
    rotacInicVolantAxis = objVolante.transform.localEulerAngles.x;
    pausa;
    case Tipos.GirarEmY:
    rotacInicVolantAxis = objVolante.transform.localEulerAngles.y;
    pausa;
    case Tipos.GirarEmZ:
    rotacInicVolantAxis = objVolante.transform.localEulerAngles.z;
    pausa;
    }
    }
    }

    void Update () {
    if (objVolante) {
    float velMultplic = 1;
    if (inverterGiro) {
    velMultplic = -1;
    }
    float direcaoFixVolante = Input.GetAxis ("Horizontal") * velMultplic;
    angulo2Volante = Mathf.MoveTowards (angulo2Volante, direcaoFixVolante, velGiroVolante * Time.deltaTime);

    switch (Rotacao) {
    case Tipos.GirarEmX:
    objVolante.transform.localEulerAngles = new Vector3 (rotacInicVolantAxis + (angulo2Volante * 540.0f), objVolante.transform.localEulerAngles.y, objVolante.transform.localEulerAngles.z); // 540 = 1,5 voltas
    pausa;
    case Tipos.GirarEmY:
    objVolante.transform.localEulerAngles = new Vector3 (objVolante.transform.localEulerAngles.x, rotacInicVolantAxis + (angulo2Volante * 540.0f), objVolante.transform.localEulerAngles.z);
    pausa;
    case Tipos.GirarEmZ:
    objVolante.transform.localEulerAngles = new Vector3 (objVolante.transform.localEulerAngles.x, objVolante.transform.localEulerAngles.y, rotacInicVolantAxis + (angulo2Volante * 540.0f));
    pausa;
    }
    }
    }
    }[/código]
     
  19. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,783
  20. mateusschw

    mateusschw

    Joined:
    Aug 27, 2020
    Posts:
    7
    Então, o que não consigo entender é a parte de atribuir o volante a ele e chamar sua função GetClampedValue ou GetAngle. Eu faço, por exemplo, "SteeringWheel.steerAngle = Mathf.Clamp ((wheelAngle / 5.0f), -40.0f, 40.0f);". ?
     
  21. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,783
  22. mateusschw

    mateusschw

    Joined:
    Aug 27, 2020
    Posts:
    7
    Não não, esse script que mostrei é um segundo que encontrei em um fórum, só mostrei porque é o que funciona melhor aqui. Mas eu estava falando sobre a minha dúvida
    na atribuição do volante e, principalmente, na chamada dessa função GetAngle () ou GetClampedValue (), no seu script desde o início da página. De qualquer forma, vou estudar esse passeio com mais calma, pois não leva meu roteiro de controle do carro. Obrigado!
     
  23. mateusschw

    mateusschw

    Joined:
    Aug 27, 2020
    Posts:
    7
    Consegui fazer funcionar ... era uma coisa muito simples que eu não estava prestando atenção. E que eu tinha um script para Input da direção do meu carro, só tive que mudar o Input para Simpleinput. rs ... vou tentar fazer os botões acelerar e travar. :):)
     
    yasirkula likes this.
  24. mateusschw

    mateusschw

    Joined:
    Aug 27, 2020
    Posts:
    7
    desculpe, eu aqui novamente. Mas eu tenho um problema ao implementar os botões de aceleração e freio em meu projeto. Você poderia me dizer se há algum lugar que explique isso? ... As poucas coisas que encontrei não funcionaram.
     
  25. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,783
    You simply add a ButtonInputUI or KeyInputUI component to the target buttons, configure the Button/Key in Inspector, remove the Button component and use SimpleInput.GetButton/SimpleInput.GetKey in your script.
     
  26. mateusschw

    mateusschw

    Joined:
    Aug 27, 2020
    Posts:
    7
    Portanto, tenho dois scripts, um que controla o carro e outro que tem as entradas "Horizontal" "Vertical", e ambos interagem entre si. Eu teria que criar um script para os botões?

    [code = CSharp] using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Carro_Input: MonoBehaviour
    {

    Carro carro;

    void Start ()
    {
    carro = GetComponent <Carro> ();
    }
    public void Update ()
    {
    carro.gui = SimpleInput.GetAxis ("Horizontal");
    carro.acc = SimpleInput.GetAxis ("Vertical");
    }
    }
    [/código]
    Este é o código de entrada. A entrada simples é o que eu mudei para trabalhar na roda antes.
     
  27. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,783
    Please check out the example scene that comes with the plugin because it has both a steering wheel and some buttons, similar to your setup.
     
    mateusschw likes this.
  28. mateusschw

    mateusschw

    Joined:
    Aug 27, 2020
    Posts:
    7
    Muito obrigado, sou totalmente novo em programação e Unity, funcionou. Acho que vou pagar por alguém agendado para fazer isso por mim lol.:)
     
  29. Deleted User

    Deleted User

    Guest

    how to attach steering wheel with script
     
  30. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,783
    To get a value from the steering wheel, you declare
    public SteeringWheel steeringWheel;
    variable in your own script and read steeringWheel.GetClampedValue() or steeringWheel.GetAngle(). If you prefer, you can also use SimpleInput plugin which has a built-in steering wheel prefab.
     
  31. unity_452

    unity_452

    Joined:
    Feb 16, 2021
    Posts:
    2
    Hi, I use PyQt5 Python when the image is facing the steering wheel. What is the code?
     
  32. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,783
    This code will work on only Unity 3D game engine, sorry.
     
  33. unity_452

    unity_452

    Joined:
    Feb 16, 2021
    Posts:
    2
    :(:(:(:(,,,,,
     
  34. wiganda

    wiganda

    Joined:
    Feb 24, 2018
    Posts:
    14
    was wondering if you want to make the car steer by itself based on a target position for the car to steer to, how would you get the steering wheel angle to turn to? ie; get the -1,1 value for the steering wheel.
     
  35. yasirkula

    yasirkula

    Joined:
    Aug 1, 2011
    Posts:
    2,783
    You can call GetClampedValue(). P.S. Sorry I misunderstood. I don't know how to estimate a target rotation for the steering wheel, unfortunately.
     
  36. Cuttlas-U

    Cuttlas-U

    Joined:
    Apr 11, 2017
    Posts:
    38
    I once used this in 2013 and now back to it in 2022 ... still works like a charm :)
     
    yasirkula likes this.