Hi.
I am new to Unity and game dev in general and I am working on a first project.
I have an issue that I cannot for the life of me fix.
I am making a script for a vehicule control (a forklift), I am using the new input system from unity and I managed to set up my actions in my script with the Invoke Unity Events behavior of the player Input component.
I decided to put the script and player input component on a dedicated gameobject called PlayerInputManager (not the vehicule itself).
Here is a first screenshot to show what I mean :
I started to setup my game mostly by following this tutorial and then I kind of went on my own way.
Now the issue is within the script itself and how I want the forward / backward movements to react in game with different braking types.
I have three buttons for this linked to three methods : MoveForward, MoveBackward and HandBrake.
Those methods are linked to a big method (maybe to big) called ApplyMotorTorque that should describe what pressing those button do.
Right now the vehicule move forward / backward, have a strong brake force with the handbrake it turn so that forks fine.
There is a last thing I want to implement for a last bit of realistic feelling wich is :
If the vehicule is moving forward and I press the moveBackward button I want to apply a specific braking force to simulate the effect of pressing the brake pedal of the vehicule (same for the other way around).
For that I am making different if / else if statements that should say, take the velocity of the forklift and if I press the button that make the vehicule go in the opposite direction applies brakeForce (I set up the force in the inspector).
But for some reason I think I have a problem with how the orientation of the velocity is checked, it seems to only want to take into consideration the global orientation for the velocity (north south east west) and not the local velocity of the forklift who should stay on the z axis no matter wich way I turn.
Meaning it applies the braking system only when my vehicule is facing in the right way, which is very frustrating.
It’s been two days now, I have tried different way to approach the problem, I asked different ai to help me with proper grammar and how to put my ideas in places and now I am not sure of what I am writing anymore…
I need help from real people to understand where my problem is .
Here is the method I made where this foward/backwar/braking system happens :
void ApplyMotorTorque()
{
// Ensure we are referencing the correct Rigidbody component for the Forklift
Rigidbody forkliftRigidBody = this.forkliftRigidBody; // Assuming forkliftRigidBody is assigned properly in the inspector
// Get the forklift's velocity in world space
Vector3 worldVelocity = forkliftRigidBody.velocity;
// Convert world velocity to local velocity of the Forklift
Vector3 localVelocity = transform.InverseTransformDirection(worldVelocity);
// Extract the forward speed (along the local Z axis)
float forwardSpeed = localVelocity.z; // Forward or backward speed
// Calculate current speed (magnitude of velocity)
float currentSpeed = forkliftRigidBody.velocity.magnitude; // Total speed of the forklift
float motorTorque = 0f;
float brakeTorque = 0f;
// max speed cap
if (currentSpeed < maxSpeed)
{
motorTorque = motorPower * accelerateInput;
}
else if (currentSpeed >= maxSpeed)
{
// Apply a small torque to maintain speed close to maxSpeed
motorTorque = motorPower * 0.1f; // Apply 10% of the motor power, adjust as needed
}
// If the handbrake is applied, override normal brake and motor torques
if (handBrakeInput > 0f)
{
ApplyHandBrake(); // This applies handBrakeTorque
return; // Exit function
}
// Case 3: Forward Movement with Backward Input (e.g., conflicting inputs)
if (forwardSpeed > 0f && moveBackwardInput > 0f)
{
motorTorque = 0f; // No movement
brakeTorque = brakePower * moveBackwardInput;
Debug.Log("Brake forward");
}
// Case 4: Backward Movement with Forward Input (conflicting inputs)
else if (forwardSpeed < 0f && accelerateInput > 0f)
{
motorTorque = 0f;
brakeTorque = brakePower * accelerateInput;
Debug.Log("Brake backward");
}
// Case 1: Moving Forward
else if (accelerateInput > 0f && moveBackwardInput == 0f)
{
if (currentSpeed < maxSpeed)
{
motorTorque = motorPower * accelerateInput;
}
brakeTorque = 0f;
}
// Case 2: Moving Backward
else if (moveBackwardInput > 0f && accelerateInput == 0f)
{
if (currentSpeed < maxSpeed) // Assuming similar logic for reverse speed limit
{
motorTorque = -reversePower * moveBackwardInput;
}
brakeTorque = 0f;
}
// Case 5: No Movement Input
else if (accelerateInput == 0f && moveBackwardInput == 0f)
{
motorTorque = 0f;
brakeTorque = brakePower / 10f;
}
// Apply the calculated motor and brake torque
ApplyTorqueWheels(motorTorque);
ApplyBrakeWheels(brakeTorque);
}
I am pretty sure I am not setting the local orientation of the forklift correctly but I can’t find how to do it on my own.
I can provide more info if needed.
Thanks !