Question by
HideOnBushe · Apr 18, 2021 at 11:56 AM ·
uibutton3dmobilebutton trigger events
How do I control my car with UI buttons?
I have a car controller witch works with a keyboard, but I want to make my game playable on mobile and I can not get the buttons to work and I am stuck.
here are the scripts that are controlling the car
public Transform centerOfMass;
public float motorTorque = 1500f;
public float maxSteer = 20f;
public float Steer { get; set; }
public float Throttle { get; set; }
private Rigidbody rb;
private Wheel[] wheels;
public float ThrottleInput { get; private set; }
public float SteerInput { get; private set; }
void Start()
{
wheels = GetComponentsInChildren<Wheel>();
rb = GetComponent<Rigidbody>();
rb.centerOfMass = centerOfMass.localPosition;
}
void Update()
{
Throttle = Input.GetAxis("Vertical");
Steer = Input.GetAxis("Horizontal");
foreach (var wheel in wheels)
{
wheel.steerAngle = Steer * maxSteer;
wheel.Torque = Throttle * motorTorque;
}
}
This one is on the cars parent
public bool Steer;
public bool invertSteer;
public bool power;
public float steerAngle { get; set; }
public float Torque { get; set; }
private WheelCollider wheelCollider;
private Transform WheelTransform;
void Start()
{
wheelCollider = GetComponentInChildren<WheelCollider>();
WheelTransform = GetComponentInChildren<MeshRenderer>().GetComponent<Transform>();
}
void Update()
{
wheelCollider.GetWorldPose(out Vector3 pos, out Quaternion rot);
WheelTransform.position = pos;
WheelTransform.rotation = rot;
}
private void FixedUpdate()
{
if (Steer)
{
wheelCollider.steerAngle = steerAngle * (invertSteer ? -1 : 1);
}
if (power)
{
wheelCollider.motorTorque = Torque;
}
}
And this one is on the wheels
I have also tried to switch to the new input system but that made more problems so if it is possible I would like to also keep it in the old input system.
Comment