Answer the question
In order to leave comments, you need to log in
How to implement car control by UI buttons?
I can not make control through the UI. The problem is with the axes, as it seems to me. Tried to define verticalAxis and transform.localPosition.z and Vector3.forward.z and many more but it doesn't work. When you press the forward button, the car goes backwards, but if you turn it around, it will go backwards, wagging itself in different directions. What is documented is a PC control.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controller : MonoBehaviour
{
private Rigidbody rb;
[SerializeField] private Transform frontRightWheelTransform;
[SerializeField] private Transform frontLeftWheelTransform;
[SerializeField] private Transform rearRightWheelTransform;
[SerializeField] private Transform rearLeftWheelTransform;
[SerializeField] private WheelCollider frontRightWheelCollider;
[SerializeField] private WheelCollider frontLeftWheelCollider;
[SerializeField] private WheelCollider rearRightWheelCollider;
[SerializeField] private WheelCollider rearLeftWheelCollider;
[SerializeField] private AudioSource engineAudioSourse;
[SerializeField] private int motorTorque;
[SerializeField] private int steerAngle;
private float currentMotorTorque;
private float currentSteerAngle;
private float horizontalAxis;
private float verticalAxis;
private bool ButtonForwardIsPressed;
private bool ButtonBackwardIsPressed;
private bool ButtonRightIsPressed;
private bool ButtonLeftIsPressed;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
GetAxises();
HandleMotor();
HandleSteerAngle();
EngineHandler();
}
private void GetAxises()
{
//horizontalAxis = Input.GetAxis("Horizontal");
//verticalAxis = Input.GetAxis("Vertical");
verticalAxis = rb.transform.position.z;
horizontalAxis = rb.transform.position.x;
}
private void HandleMotor()
{
//currentMotorTorque = motorTorque * verticalAxis;
//frontRightWheelCollider.motorTorque = currentMotorTorque;
//frontLeftWheelCollider.motorTorque = currentMotorTorque;
//rearRightWheelCollider.motorTorque = currentMotorTorque;
//rearLeftWheelCollider.motorTorque = currentMotorTorque;
if (ButtonForwardIsPressed == true)
{
currentMotorTorque = motorTorque * verticalAxis * -1;
frontRightWheelCollider.motorTorque = currentMotorTorque;
frontLeftWheelCollider.motorTorque = currentMotorTorque;
rearRightWheelCollider.motorTorque = currentMotorTorque;
rearLeftWheelCollider.motorTorque = currentMotorTorque;
}
else
{
return;
}
if (ButtonBackwardIsPressed == true)
{
currentMotorTorque = motorTorque * verticalAxis;
frontRightWheelCollider.motorTorque = currentMotorTorque;
frontLeftWheelCollider.motorTorque = currentMotorTorque;
rearRightWheelCollider.motorTorque = currentMotorTorque;
rearLeftWheelCollider.motorTorque = currentMotorTorque;
}
else
{
return;
}
}
private void HandleSteerAngle()
{
//currentSteerAngle = steerAngle * horizontalAxis;
//frontRightWheelCollider.steerAngle = currentSteerAngle;
//frontLeftWheelCollider.steerAngle = currentSteerAngle;
if (ButtonRightIsPressed == true)
{
currentSteerAngle = steerAngle * rb.transform.right.x;
frontRightWheelCollider.steerAngle = currentSteerAngle;
frontLeftWheelCollider.steerAngle = currentSteerAngle;
}
else
{
return;
}
if (ButtonLeftIsPressed == true)
{
currentSteerAngle = steerAngle * rb.transform.right.x * -1;
frontRightWheelCollider.steerAngle = currentSteerAngle;
frontLeftWheelCollider.steerAngle = currentSteerAngle;
}
else
{
return;
}
}
private void EngineHandler()
{
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S))
{
if (!engineAudioSourse.isPlaying)
{
engineAudioSourse.Play();
}
}
else
{
engineAudioSourse.Stop();
}
}
#region UIButtons
public void OnForwardButtonDown()
{
ButtonForwardIsPressed = true;
}
public void OnBackwardButtonDown()
{
ButtonBackwardIsPressed = true;
}
public void OnRightButtonDown()
{
ButtonRightIsPressed = true;
}
public void OnLeftButtonDown()
{
ButtonLeftIsPressed = true;
}
public void OnForwardButtonUp()
{
ButtonForwardIsPressed = false;
}
public void OnBackwardButtonUp()
{
ButtonBackwardIsPressed = false;
}
public void OnRightButtonUp()
{
ButtonRightIsPressed = false;
}
public void OnLeftButtonUp()
{
ButtonLeftIsPressed = false;
}
#endregion
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question