- Home /
Question by
Salty_Lightning · Dec 28, 2021 at 06:09 AM ·
movementspeed
I need help with making my player move faster (3D Project)
I'm new to coding with c# and I need help with making my player move faster. I tried using the same code that my jump button uses but change "up" to "forward" and "VelocityChange" to "Acceleration". Can you tell me what's wrong and how to fix it?
// 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");
//To make the play move a certain speed
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);
}
}
}
Comment
Answer by BurakKaya · Dec 28, 2021 at 06:30 AM
I am assuming you didn't try this :
public float acceleration;
private void FixedUpdate(){
//Code
if (wKeyWasPressed)
{
rigidbodyComponent.AddForce(new Vector3.forward * acceleration, ForceMode.Acceleration);
// Changed hardcoded value 5 to a variable that you can edit in Inspector.
}
}