- Home /
Drive car with Pedals
I need to be able to drive a car like an electric vehicle with an automatic gearbox in Unity . I have a Pedal for acceleration and a Pedal for Brake, both have an output from -1 to 1.
The Pedal for acceleration works correctly. But when I press Brake, the car will Brake and then it drives backwards. I have tryed to code the Breake Pedal to "isBreaking = Input.GetKey(KeyCode.Space);" but i get a error because the Pedal ist not a Button, so the output is from -1 to 1.
So my question is, 1. How I have to code the Brake right, so the car apply only Breake, if Brake Pedal is pressed. 2. How can I code for driving Backwards like a gearshift logik. I want to go into reverse gear with the "r" button and then press the accelerator pedal, and the car should then go in reverse. If I then press the "f" button, the gear shift should be set back to forward and drive forward.
The code from Youtube (https://www.youtube.com/watch?v=Z4HA8zJhGEk)
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarController : MonoBehaviour
{
private const string HORIZONTAL = "Horizontal";
private const string VERTICAL = "Vertical";
private float horizontalInput;
private float verticalInput;
private float currentSteerAngle;
private float currentbreakForce;
private bool isBreaking;
[SerializeField] private float motorForce;
[SerializeField] private float breakForce;
[SerializeField] private float maxSteerAngle;
[SerializeField] private WheelCollider frontLeftWheelCollider;
[SerializeField] private WheelCollider frontRightWheelCollider;
[SerializeField] private WheelCollider rearLeftWheelCollider;
[SerializeField] private WheelCollider rearRightWheelCollider;
[SerializeField] private Transform frontLeftWheelTransform;
[SerializeField] private Transform frontRightWheeTransform;
[SerializeField] private Transform rearLeftWheelTransform;
[SerializeField] private Transform rearRightWheelTransform;
private void FixedUpdate()
{
GetInput();
HandleMotor();
HandleSteering();
UpdateWheels();
}
private void GetInput()
{
horizontalInput = Input.GetAxis(HORIZONTAL);
verticalInput = Input.GetAxis(VERTICAL);
isBreaking = Input.GetKey(KeyCode.Space);
}
private void HandleMotor()
{
frontLeftWheelCollider.motorTorque = verticalInput * motorForce;
frontRightWheelCollider.motorTorque = verticalInput * motorForce;
currentbreakForce = isBreaking ? breakForce : 0f;
ApplyBreaking();
}
private void ApplyBreaking()
{
frontRightWheelCollider.brakeTorque = currentbreakForce;
frontLeftWheelCollider.brakeTorque = currentbreakForce;
rearLeftWheelCollider.brakeTorque = currentbreakForce;
rearRightWheelCollider.brakeTorque = currentbreakForce;
}
private void HandleSteering()
{
currentSteerAngle = maxSteerAngle * horizontalInput;
frontLeftWheelCollider.steerAngle = currentSteerAngle;
frontRightWheelCollider.steerAngle = currentSteerAngle;
}
private void UpdateWheels()
{
UpdateSingleWheel(frontLeftWheelCollider, frontLeftWheelTransform);
UpdateSingleWheel(frontRightWheelCollider, frontRightWheeTransform);
UpdateSingleWheel(rearRightWheelCollider, rearRightWheelTransform);
UpdateSingleWheel(rearLeftWheelCollider, rearLeftWheelTransform);
}
private void UpdateSingleWheel(WheelCollider wheelCollider, Transform wheelTransform)
{
Vector3 pos;
Quaternion rot;
wheelCollider.GetWorldPose(out pos, out rot);
wheelTransform.rotation = rot;
wheelTransform.position = pos;
}
}