Скрипт управление машин в конках видом с верху

Пытаюсь написать скрипт управления машиной. Вот мой скрипт:

using System.Collections;
using System.Collections.Generic; 
using UnityEngine;

public class FinalCarControl : MonoBehaviour
{
 Rigidbody2D rb;

public float speedCorrect;
public float maxSpeed;

//Педаль газа
float throttleInput = 0f;
//педаль тормоза
float breakInput = 0f;
//положение руля
float steerInput = 0f;

float targetSpeed = 0;
float khm = 0f;
public AnimationCurve sccel;


/// <summary>
/// Ухудшение манёвренности
/// </summary>

float maxRotAngle = 120f;
float goodSpeed = 3f;
float deltaRot = 3f;
float RotAngle = 0f;

// Start is called before the first frame update
void Start()
{
    rb = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update()
{
    targetSpeed = rb.velocity.magnitude;

    khm = targetSpeed;

    throttleInput = Input.GetAxis("Vertical");
    steerInput = Input.GetAxis("Horizontal");

    //торможение
    if (Input.GetKey(KeyCode.Space))
    {
        rb.velocity = Vector2.Lerp(rb.velocity, Vector2.zero, Time.deltaTime * 500);
    }
}

private void FixedUpdate()
{
    //задание скорости автомобиля
    if (throttleInput != 0f)
        
        rb.velocity += speedCorrect * throttleInput * (Vector2)transform.up * sccel.Evaluate(targetSpeed / maxSpeed);
    
       

    //поворт
    if (steerInput != 0f && targetSpeed >=0.1)
    {
        if (targetSpeed <= goodSpeed)
        {
            RotAngle = maxRotAngle;
        }
        else
            RotAngle = maxRotAngle - Mathf.Lerp(0f, deltaRot, (targetSpeed - goodSpeed) / (maxSpeed - goodSpeed));
        
        float turn = RotAngle * steerInput * Time.deltaTime;
        
        transform.Rotate(0f, 0f, -turn);
        Vector2 oldDir = rb.velocity / targetSpeed;
        
        Vector2 newDir = (Vector2)transform.up + oldDir;
        
        newDir.Normalize();
        rb.velocity = targetSpeed * newDir;

       
    }
    
    

    if(throttleInput == 0f)
    {
        rb.velocity = Vector2.Lerp(rb.velocity, Vector2.zero,  Time.deltaTime);
    }
}

private void OnGUI()
{
    GUI.TextField(new Rect(10, 10, 120, 20), khm + " km");
}

}

У меня возникли следующие проблемы:

  1. Когда двигаюсь назад и пытаюсь повернуть машина начинает поворачиваться так как будто я двигаюсь вперёд.
  2. Когда зажимаю кнопку поворота и хочу прибавить скорости кнопка газа не реагирует.

Знаете ли вы способы решения моих проблем?


Ответы (0 шт):