So I am following a tutorial by Pretty Fly Games on how to make a 2D racing game and I have added the car controller and the car input handler like he did in the video but my car cannot move forward, only rotate.
Here is a link to the tutorial: https://www.youtube.com/watch?v=DVHcOS1E5OQ&list=PLyDa4NP_nvPfmvbC-eqyzdhOXeCyhToko.
I have no idea what to do. It works normally on his video but not in my project.
Here is the Car Controller:
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<Rigidbody2D>();
}
// 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;
}
}
And here is the car input handler:
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<CarController>();
}
// 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);
}
}