Godot Version
4.5.1
Question
I am working on adding vehicles to my game, I have made a test vehicle using the VehicleBody3D, four wheels and a standard mesh and collision shape.
I have a script that assigns which wheels are giving traction based on a select drive mode (AWD, RWD and FWD), and that’s about it
But so far, every time I start the game, the car spins randomly for a few seconds, and then whenever I try and drive it will spin or turn randomly with no input other than pressing forward.
Below you can find the code that controls the car, and a video of what happens when I start the game and when I try to accelerate. I have put a label in the top left of the video that tells you at what point I start to accelerate.
Thank you in advance for any help, and let me know if you need any more information.
Car Code
using System;
using Godot;
namespace CityGame.Scenes.Cars.VTestCar;
public partial class Car : VehicleBody3D
{
[ExportCategory("Engine Configuration")]
[Export] private float _enginePower;
[Export] private float _maxSpeed;
[ExportCategory("Wheel Configuration")]
[Export] private float _brakePower;
[Export] private float _maxWheelTurn;
[Export] private CarTypeEnum _wheelPowerType;
[Export] private bool _isBeingControlled;
private Camera3D _camera;
[Export] private VehicleWheel3D _frontLeft;
[Export] private VehicleWheel3D _frontRight;
[Export] private VehicleWheel3D _rearLeft;
[Export] private VehicleWheel3D _rearRight;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_camera = GetNode<Camera3D>("CarThirdPersonCamera");
switch (_wheelPowerType)
{
case CarTypeEnum.RWD:
_rearLeft.UseAsTraction = true;
_rearRight.UseAsTraction = true;
_frontLeft.UseAsTraction = false;
_frontRight.UseAsTraction = false;
break;
case CarTypeEnum.AWD:
_rearLeft.UseAsTraction = true;
_rearRight.UseAsTraction = true;
_frontLeft.UseAsTraction = true;
_frontRight.UseAsTraction = true;
break;
case CarTypeEnum.FWD:
_rearLeft.UseAsTraction = false;
_rearRight.UseAsTraction = false;
_frontLeft.UseAsTraction = true;
_frontRight.UseAsTraction = true;
break;
default:
_wheelPowerType = CarTypeEnum.AWD;
_rearLeft.UseAsTraction = true;
_rearRight.UseAsTraction = true;
_frontLeft.UseAsTraction = true;
_frontRight.UseAsTraction = true;
break;
}
_frontRight.UseAsSteering = true;
_frontLeft.UseAsSteering = true;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
float movementVector = Input.GetAxis("turn_left", "turn_right");
if (movementVector > 0)
{
Steering = -0.3f;
}
else if (movementVector < 0)
{
Steering = 0.3f;
}
else
{
Steering = 0;
}
if (Input.IsActionPressed("accelerate"))
{
EngineForce = _enginePower;
}
else
{
EngineForce = 0;
}
if (Input.IsActionJustPressed("brake"))
{
Brake = _brakePower;
}
else
{
Brake = 0;
}
if (!_isBeingControlled)
{
EngineForce = 0;
Brake = 0;
_camera.Current = false;
}
else
{
_camera.Current = true;
}
}
}