- Home /
Question about movement with Rigidbody | Diagnol movement going faster
Hello all, I am fairly new to c# and am working on a Rigidbody character script, but am trying to find out how to make sure the player doesn't speed up when going forward/backward and strafing at the same time.
What would I need to look into to fix this issue? Any and all help is welcome, I just need a point in the right direction to start studying more, thanks in advance!
void Update()
{
xInput = Input.GetAxis("Horizontal") * strafeSpeed;
yInput = Input.GetAxis("Vertical") * walkSpeed;
playerMovement = new Vector3(xInput, 0.0f, yInput);
}
void FixedUpdate() {
rb.velocity = new Vector3(playerMovement.x, rb.velocity.y, playerMovement.z);
}
}
Answer by JustAbhi · Oct 26, 2021 at 04:59 AM
Just normalize the vector before assigning the velocity. Something like this should work.
void FixedUpdate() {
rb.velocity = new Vector3(playerMovement.x, rb.velocity.y, playerMovement.z).normalize; //here normalize the vector
}
Hope this helps.
Another approach to this is to apply it to the playerMovement input itself, and only if necessary (for example, this accommodates analog stick control and similar):
playerMovement = new Vector3(xInput, 0.0f, yInput);
if(playerMovement.sqrMagnitude > 1.0f)
{
playerMovement.Normalize();
}
This would also prevent existing Y-axis speed from being factored in.
Thank you for the suggestion, but when I did that it made the character extremely slow in every direction, so I am wondering if I am missing a step as well?
As long as it is the same speed in all the directions, you can simply multiply the speed by whatever factor you need to get it right, can't you? Take the normalized vector and multiply it by whatever factor makes it move at the right speed.
You'd need to make sure that you multiply by speed values *after* normalizing the vector. For an example which accommodates different speeds for forward/backward and strafing:
float maxSpeed;
void Start()
{
maxSpeed = Mathf.Max(walkSpeed, strafeSpeed);
}
void Update()
{
xInput = Input.GetAxis("Horizontal") * strafeSpeed;
yInput = Input.GetAxis("Vertical") * walkSpeed;
playerMovement = new Vector3(xInput, 0.0f, yInput);
if(playerMovement.sqrMagnitude > maxSpeed)
{
playerMovement = playerMovement.normalized * maxSpeed;
}
}
Full accommodation of different forward and backward speeds would call for a different maximum speed per direction, with a condition necessary to check which to use:
if(Vector3.Dot(Vector3.forward, playerMovement) >= 0)
{
// moving forward
}
else //if(Vector3.Dot(Vector3.forward, playerMovement) < 0)
{
// moving backward
}
Answer by JSoller · Oct 26, 2021 at 11:16 PM
Thank you everyone for the help! I was adding some of the code to the wrong areas and managed to fix it by adding the if statement from above! Very cool, I appreciate the guidance!
Your answer
Follow this Question
Related Questions
Why does my character keep sliding around? Help! (Rigid Body) 1 Answer
Making a bubble level (not a game but work tool) 1 Answer
(C#) Movement Rotation Script not Working! 0 Answers
Instantiated Bullet Lags Behind Animated Timeline Object 1 Answer
How to make the player move a fixed distance? (Tile per tile) 0 Answers