Hi folks, I recently updated my unity version from 2021.3.26 to 2022.3.8f1 and converted an existing project.
Now my physics movement system is acting unexpectedly.
It previously worked normally, I could move forward, backwards, strafe left and right with WASD keys.
Now strafing (A,D) moves in small circles and moving forward and backward (W,S) moves in different directions relative to the direction the player is facing. Below is my FixedUpdate
method of the controlling script. jump
, controlForward
and strafe
variables are floats set by Input.GetAxis()
in the update method.
I am using a capsule collider with physics to move.
Please advise if I am doing something incorrectly here or if you need clarification on some other part of my code or design.
void FixedUpdate()
{
float forceForward = 0f;
float forceSideways = 0f;
if (grounded)
{
if (jump != 0 )
{
if (jumptimer > jumpdelay) {
jumptimer = 0;
rb.AddForce(Vector3.up * jumpforce + (controlForward * mychar.walkspeed * transform.forward), ForceMode.Impulse);
Debug.Log("jumping from " + standingOn.name);
}
anim.SetBool("jumping", true);
anim.SetInteger("moving", 0);
}
else if (strafe != 0.0f || controlForward != 0.0f)
{
if (controlForward != 0.0f) //strafe != 0.0f ||
{
forceForward = (mychar.walkspeed + (run * mychar.runspeed)) * controlForward;
rb.AddForce(forceForward * transform.forward, ForceMode.VelocityChange);
//rb.AddRelativeForce(forceForward * transform.forward, ForceMode.VelocityChange);
}
if (strafe != 0.0f)//|| forward != 0.0f)
{
forceSideways = (mychar.walkspeed + (run * mychar.runspeed)) * 0.75f * strafe;
rb.AddForce(forceSideways * transform.right, ForceMode.VelocityChange);
//rb.AddRelativeForce(forceSideways * transform.right, ForceMode.VelocityChange);
}
anim.SetInteger("moving", 1);
anim.SetBool("jumping", false);
}
else
{
anim.SetInteger("moving", 0);
anim.SetBool("jumping", false);
}
}
else
{
if (jump != 0 && jumptimer > jumpdelay)
{
jumptimer = 0;
Debug.Log("not standing on anything to jump from");
}
}
grounded = false;
}