I’m trying to make my player move faster in my project and I thought this code should work but it didn’t. I tried copying with the same code my jump button uses but just change “up” to “forward” and “VelocityChange” to “acceleration”.
// Start is called before the first frame update
void Start()
{
rigidbodyComponent = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
// Check if Space key jumps the player
if (Input.GetKeyDown(KeyCode.Space))
{
SpaceKeyWasPressed = true;
}
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
if (Input.GetKeyDown(KeyCode.W))
{
wKeyWasPressed = true;
}
}
//FixedUpdate is called once every update
private void FixedUpdate()
{
if (Physics.OverlapSphere(groundCheckTransform.position, 0.1f).Length == 1)
{
return;
}
// The Jump Code to make my player jump
if (SpaceKeyWasPressed)
{
rigidbodyComponent.AddForce(Vector3.up * 5, ForceMode.VelocityChange);
SpaceKeyWasPressed = false;
}
rigidbodyComponent.velocity = new Vector3(horizontalInput, rigidbodyComponent.velocity.y, verticalInput);
//make player move a certain speed
if (wKeyWasPressed)
{
rigidbodyComponent.AddForce(new Vector3.forward * 5, ForceMode.Acceleration);
}
}
}