Hello,
I have a problem in my Unity Project. My character when moving shakes a little bit on the y axis (more precisely on his transform.up axis).
For a little context, the project I’m working on has as the main mechanic the power to change gravity.
So the player changes his transform.up to walk on the walls or the roof.
Here is the code I wrote to move the player.
private void FixedUpdate()
{
if (!isGravityStop)
{
Vector2 moveDir = moveAction.ReadValue<Vector2>();
Vector3 velocity = new Vector3();
if (isGrounded)
velocity = transform.forward * moveDir.y * speed + transform.right * moveDir.x * speed;
else
velocity = transform.forward * moveDir.y * speed / airFriction + transform.right * moveDir.x * speed / airFriction;
rb.velocity = new Vector3(
Math.Abs(transform.up.x) * rb.velocity.x,
Math.Abs(transform.up.y) * rb.velocity.y,
Math.Abs(transform.up.z) * rb.velocity.z
) + velocity;
rb.AddForce(new Vector3(
Mathf.Clamp(transform.up.x * Physics.gravity.y * fallSpeed, -maxVelocity, maxVelocity),
Mathf.Clamp(transform.up.y * Physics.gravity.y * fallSpeed, -maxVelocity, maxVelocity),
Mathf.Clamp(transform.up.z * Physics.gravity.y * fallSpeed, -maxVelocity, maxVelocity)
), ForceMode.Acceleration);
}
So as you can see I re simulate the gravity of the player and I use the rigidbody and the velocity to move the player around.
The way the movement is working is exactly the way I want but there this little problem that bugging me off.
You can see how the game works in this video and you can also see the little shaking on the “transform.up axis” of the player.
I already tried the Interpolate of the rigidbody but it does not work with the rotation of the player and the camera.
How can I solve this problem ?
Thank you for your help
If you have more than one or two dots (.) in a single statement, you’re just being mean to yourself.
Putting lots of code on one line DOES NOT make it any faster. That’s not how compiled code works.
The longer your lines of code are, the harder they will be for you to understand them.
How to break down hairy lines of code:
http://plbm.com/?p=248
Break it up, practice social distancing in your code, one thing per line please.
“Programming is hard enough without making it harder for ourselves.” - angrypenguin on Unity3D forums
“Combining a bunch of stuff into one line always feels satisfying, but it’s always a PITA to debug.” - StarManta on the Unity3D forums
After you simplify the code, you might stand a chance of debugging it.
By debugging you can find out exactly what your program is doing so you can fix it.
Use the above techniques to get the information you need in order to reason about what the problem is.
You can also use Debug.Log(...);
statements to find out if any of your code is even running. Don’t assume it is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.
Thank you a lot, I manage to find the cause of the problem and a solution to make it work.
private void FixedUpdate()
{
if (!isGravityStop)
{
Vector2 moveDir = moveAction.ReadValue<Vector2>();
float speedModified = speed;
if (!isGrounded)
speedModified = speed / airFriction;
Vector3 forwardMovement = transform.forward * moveDir.y;
forwardMovement *= speedModified;
Vector3 rightMovement = transform.right * moveDir.x;
rightMovement *= speedModified;
Vector3 velocity = forwardMovement;
velocity += rightMovement;
Vector3 keepVelocity = new Vector3(
Mathf.Abs(transform.up.x) * rb.velocity.x,
Mathf.Abs(transform.up.y) * rb.velocity.y,
Mathf.Abs(transform.up.z) * rb.velocity.z
);
velocity += keepVelocity;
rb.velocity = velocity;
float gravityToApply = Physics.gravity.y * fallSpeed;
float xGravity = transform.up.x * gravityToApply;
xGravity = Mathf.Clamp(xGravity, -maxVelocity, maxVelocity);
float yGravity = transform.up.y * gravityToApply;
yGravity = Mathf.Clamp(yGravity, -maxVelocity, maxVelocity);
float zGravity = transform.up.z * gravityToApply;
zGravity = Mathf.Clamp(zGravity, -maxVelocity, maxVelocity);
if (isGrounded)
{
Vector3 noGravity = new Vector3(rb.velocity.x, -0f, rb.velocity.z);
rb.velocity = noGravity;
}
else
{
Vector3 gravity = new Vector3(xGravity, yGravity, zGravity);
rb.AddForce(gravity, ForceMode.Acceleration);
}
}
I broke down my code by steps as you told me and by using Debug and Breakpoint I manage to found the source of the problem and to apply a fix.
Thank you a lot, I will now try to not make super long line for no reasons.
1 Like