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
}
задокументировано - управление для пкИспользуйте #ifdef для такого, чтобы работали ОБА варианта в зависимости от платформы, без ручного пекреключения.
else
{
return;
}
rb.transform.position.z
и transform.position.z;
одно и тоже, в данном случае. И это не "ось", не "axis", а лишь координата объекта по одной из осей. Получается, что чем дальше авто проедет вперед, тем быстрее будет ехать вперед. Чем сильнее сместиться вправо, тем сильнее будет поворачивать вправо. Странно это всё.