Почему моя машина только вращается и не движется?C#

Место общения программистов C#
Anonymous
Почему моя машина только вращается и не движется?

Сообщение Anonymous »

Итак, я следую руководству Pretty Fly Games о том, как создать 2D-гоночную игру, и добавил контроллер автомобиля и обработчик ввода автомобиля, как он это сделал в видео, но моя машина не может двигаться вперед, а только вращаться.< /p>
Вот ссылка на руководство:
I понятия не имею, что делать. На его видео он работает нормально, но не в моем проекте.
Вот автомобильный контроллер:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CarController : MonoBehaviour
{
[Header("Car Settings")]
public float accelerationFactor = 30.0f;
public float turnFactor = 3.5f;

//Local Variables
float accelerationInput = 0;
float steeringInput = 0;

float rotationAngle = 0;

//Components
Rigidbody2D carRigidbody2D;

//Awake is called when the script instance is being loaded.
void Awake()
{
carRigidbody2D = GetComponent();

}

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}

// Frame-rate independet for physics calculations
void FixedUpdate()
{
ApplyEngineForce();

ApplySteering();
}

void ApplyEngineForce()
{
//Crate a force for the engine
Vector2 engineForceVector = transform.up * accelerationInput * accelerationFactor;

//Apply force and pushes the car forward
carRigidbody2D.AddForce(engineForceVector, ForceMode2D.Force);
}

void ApplySteering()
{
//Update the rotation angle based on input
rotationAngle -= steeringInput * turnFactor;

//Apply steering by rotating the car object
carRigidbody2D.MoveRotation(rotationAngle);
}

public void SetInputVector(Vector2 inputVector)
{
steeringInput = inputVector.x;
accelerationFactor = inputVector.y;
}
}

А вот обработчик ввода автомобиля:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CarInputHandler : MonoBehaviour
{
//Components
CarController topDownCarController;

//Awake is called when the script instance is being loaded.
void Awake()
{
topDownCarController = GetComponent();

}
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
Vector2 inputVector = Vector2.zero;

inputVector.x = Input.GetAxis("Horizontal");
inputVector.y = Input.GetAxis("Vertical");

topDownCarController.SetInputVector(inputVector);
}
}


Подробнее здесь: https://stackoverflow.com/questions/786 ... d-not-move

Вернуться в «C#»