how do i stop unity from keeping my button pressed?
i have a basic car controller script, the thing is whenever i hold th w or s keys for too long unity just keeps holding those keys for how long i pressed them. I also can not do brake i that time.
this is my script:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarController : MonoBehaviour
{
[Header("Transforms")]
[SerializeField] private Transform fronLeftT;
[SerializeField] private Transform fronRightT;
[SerializeField] private Transform rearLeftT;
[SerializeField] private Transform rearRightT;
[SerializeField] private WheelCollider frontleftC;
[SerializeField] private WheelCollider frontRightC;
[SerializeField] private WheelCollider rearleftC;
[SerializeField] private WheelCollider rearRightC;
[SerializeField] private float motorForce = 100f;
[SerializeField] private float maxSteeringAngle = 50f;
[SerializeField] private float breakForce = 100;
private float steering;
private float horizontalInput;
private float verticalInput;
bool isBraking;
// Call Methods
private void FixedUpdate()
{
GetInput();
accelerate();
steer();
}
// Steer
private void steer()
{
steering = horizontalInput * maxSteeringAngle;
frontleftC.steerAngle = steering;
frontRightC.steerAngle = steering;
}
// User Input Method
private void GetInput()
{
//get User Input
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
// Initialize Break
if(Input.GetKeyDown(KeyCode.Space))
{
isBraking = true;
if(isBraking == true)
{
breakCar();
}
}
if(Input.GetKeyUp(KeyCode.Space))
{
isBraking = false;
if(isBraking == false)
{
frontleftC.brakeTorque = 0f;
frontRightC.brakeTorque = 0f;
rearleftC.brakeTorque = 0f;
rearRightC.brakeTorque = 0f;
}
}
}
// brake
private void breakCar()
{
frontleftC.brakeTorque = breakForce;
frontRightC.brakeTorque = breakForce;
rearleftC.brakeTorque = breakForce;
rearRightC.brakeTorque = breakForce;
}
// Accelerate
private void accelerate()
{
frontleftC.motorTorque = verticalInput * motorForce;
frontRightC.motorTorque = verticalInput * motorForce;
}
}
Comment
Your answer
Follow this Question
Related Questions
Simple arcade vehicle turning with or w/o wheel colliders 0 Answers
How to make player move from one track to the other gradually? 1 Answer
Unity Space Shooter Tutorial Player Will Not Move 1 Answer
Unity animation running, and then just teleporting the player back to origin 0 Answers
Issue with wave-like motion when rotating with mouse input 0 Answers