@Bruh_Bruh

Как реализовать управление машиной по UI кнопкам?

Не могу сделать управление через UI. Проблема именно с осями, как мне кажется. Пробовал определять verticalAxis и transform.localPosition.z, и Vector3.forward.z, и много чего еще, но это не работает. При нажатии кнопки движения вперед машина едет назад, но если ее развернуть, то она будет ехать задом вперед, сама вилять в разные стороны. То, что задокументировано - управление для пк.
spoiler
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

}

  • Вопрос задан
  • 166 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы