- Home /
I'm trying to make my player move a certain speed I want it to go, but I'm having a hard time. (3D project)
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);
}
}
}
Answer by smkzalan · Dec 27, 2021 at 01:23 PM
You should in an update make that this will happen:
rigidbodyComponent.velocity = Vector3.ClampMagnitude(rigidbodyComponent.velocity, clampSpeed);
Your answer
Follow this Question
Related Questions
Continuous Adjustable Movement in 360 degrees 0 Answers
My Character is moving fine horizontally and vertically, but moves way too fast diagonally 2 Answers
How to move an object a specific speed in km/h? 2 Answers
How to fix horizontal movement code causing fast speed bug? 0 Answers
Moving up and down (in a slope) without change to velocity or "bumping" 0 Answers