Search Unity

Need to change from old input system Input.GetAxis("Horizontal") .. to new input system

Discussion in 'Scripting' started by Arkaj, Jun 25, 2021.

  1. Arkaj

    Arkaj

    Joined:
    Aug 9, 2020
    Posts:
    6
    I need to change from
    Input.GetAxis("Horizontal") to the new input sysem
    the script:
    ///////////////////////////////////////////////////////////////////////////////////////////////
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;


    public class RearWheelDrive : MonoBehaviour {

    private WheelCollider[] wheels;

    public float maxAngle = 30;
    public float maxTorque = 300;
    public GameObject wheelShape;

    // here we find all the WheelColliders down in the hierarchy
    public void Start()
    {
    wheels = GetComponentsInChildren<WheelCollider>();

    for (int i = 0; i < wheels.Length; ++i)
    {
    var wheel = wheels ;

    // create wheel shapes only when needed
    if (wheelShape != null)
    {
    var ws = GameObject.Instantiate (wheelShape);
    ws.transform.parent = wheel.transform;

    if(wheel.transform.localPosition.x < 0f)
    {
    ws.transform.localScale = new Vector3(ws.transform.localScale.x*-1f,ws.transform.localScale.y,ws.transform.localScale.z);
    }
    }
    }
    }

    // this is a really simple approach to updating wheels
    // here we simulate a rear wheel drive car and assume that the car is perfectly symmetric at local zero
    // this helps us to figure our which wheels are front ones and which are rear
    public void Update()
    {
    float DirX = Input.GetAxis("Horizontal") * maxAngle ;
    float DirY = Input.GetAxis("Vertical") * maxTorque ;

    foreach (WheelCollider wheel in wheels)
    {
    // a simple car where front wheels steer while rear ones drive
    if (wheel.transform.localPosition.z > 0)
    wheel.steerAngle = DirX;

    if (wheel.transform.localPosition.z < 0)
    wheel.motorTorque = DirY;

    // update visual wheels if any
    if (wheelShape)
    {
    Quaternion q;
    Vector3 p;
    wheel.GetWorldPose (out p, out q);

    // assume that the only child of the wheelcollider is the wheel shape
    Transform shapeTransform = wheel.transform.GetChild (0);
    shapeTransform.position = p;
    shapeTransform.rotation = q;
    }


    }
    }
    }
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
  2. Arkaj

    Arkaj

    Joined:
    Aug 9, 2020
    Posts:
    6
    does any one have a solution
     
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
  4. Arkaj

    Arkaj

    Joined:
    Aug 9, 2020
    Posts:
    6
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine.InputSystem;

    public class RearWheelDrive : MonoBehaviour {

    private WheelCollider[] wheels;

    public float maxAngle = 30;
    public float maxTorque = 300;
    public GameObject wheelShape;



    // here we find all the WheelColliders down in the hierarchy
    public void Start()
    { var ws = GameObject.Instantiate (wheelShape);
    wheels = GetComponentsInChildren<WheelCollider>();

    for (int i = 0; i < wheels.Length; ++i)
    {
    var wheel = wheels ;

    // create wheel shapes only when needed
    if (wheelShape != null)
    {
    var ws = GameObject.Instantiate (wheelShape);
    ws.transform.parent = wheel.transform;

    if(wheel.transform.localPosition.x < 0f)
    {
    ws.transform.localScale = new Vector3(ws.transform.localScale.x*-1f,ws.transform.localScale.y,ws.transform.localScale.z);
    }
    }
    }
    }
    public void OnMove(InputValue input)
    {
    Vector2 inputVec = input.Get<Vector2>();
    wheels = GetComponentsInChildren<WheelCollider>();

    for (int i = 0; i < wheels.Length; ++i)
    {
    var wheel = wheels ;

    // create wheel shapes only when needed
    if (wheelShape != null)
    {
    var ws = GameObject.Instantiate (wheelShape);
    ws.transform.parent = wheel.transform;

    if(wheel.transform.localPosition.x < 0f)
    {
    ws.transform.localScale = new Vector3(ws.transform.localScale.x*-1f,ws.transform.localScale.y,ws.transform.localScale.z);
    }
    }
    }


    }

    // this is a really simple approach to updating wheels
    // here we simulate a rear wheel drive car and assume that the car is perfectly symmetric at local zero
    // this helps us to figure our which wheels are front ones and which are rear
    public void Update()
    {
    float DirX = Input.GetAxisRaw("Horizontal") * maxAngle ;
    float DirY = Input.GetAxisRaw("Vertical") * maxTorque ;

    foreach (WheelCollider wheel in wheels)
    {
    // a simple car where front wheels steer while rear ones drive
    if (wheel.transform.localPosition.z > 0)
    wheel.steerAngle = DirX;

    if (wheel.transform.localPosition.z < 0)
    wheel.motorTorque = DirY;

    // update visual wheels if any
    if (wheelShape)
    {
    Quaternion q;
    Vector3 p;
    wheel.GetWorldPose (out p, out q);

    // assume that the only child of the wheelcollider is the wheel shape
    Transform shapeTransform = wheel.transform.GetChild (0);
    shapeTransform.position = p;
    shapeTransform.rotation = q;
    }

    }
    }
    }
     
  5. Arkaj

    Arkaj

    Joined:
    Aug 9, 2020
    Posts:
    6
    i changed it but i got an error : A local or parameter named 'ws' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
     
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    You know what would be great? Having line numbers to talk about.
    Both in the error message, as well as in the posted code example. If only there was a way... :(