#Snapping and Glitchy Movement when rotating player

1 messages · Page 1 of 1 (latest)

dull cradle
#
public void HandleGravity(float multiplier)
    {
        if (_slopeWalkable) _rb2D.velocity -= multiplier * _groundRay.normal * -Physics2D.gravity.y * _rb2D.mass * Time.deltaTime;

        else _rb2D.velocity -= multiplier * Vector2.up * -Physics2D.gravity.y * _rb2D.mass * Time.deltaTime;
    }

Called every FixedUpdate

#
protected void ApplyMovement(float multiplier)
    {
        float finalSpeed = _ctx.BaseMoveSpeed;

        _ctx.TargetSpeed = finalSpeed * (_ctx.InputHorizontal * multiplier);

        _ctx.SpeedDifference = _ctx.IsGrounded ? _ctx.TargetSpeed - _ctx.VelocityOld.x : _ctx.TargetSpeed - _ctx.Rb2D.velocity.x;

        float accelerationRate = (Mathf.Abs(_ctx.TargetSpeed) > 0.05f) ? _ctx.Acceleration : _ctx.Deceleration;
        float movement = _ctx.SpeedDifference * accelerationRate;

        if (_ctx.IsGrounded) _ctx.Rb2D.AddForce(movement * -_ctx.GroundPerp);

        else _ctx.Rb2D.AddForce(movement * Vector2.right);
        Debug.Log(_ctx.TargetSpeed);
    }

Called every FixedUpdate

#
public void HandleRotation()
    {
        float angle = Vector2.Angle(_groundRay.normal, Vector2.up);
        if (_slopeWalkable) this.transform.eulerAngles = new Vector3(0f, 0f, Mathf.MoveTowardsAngle(this.transform.eulerAngles.z, -angle, Time.deltaTime * 100));

        else this.transform.eulerAngles = new Vector3(0f, 0f, Mathf.MoveTowardsAngle(this.transform.eulerAngles.z, 0f, Time.deltaTime * 100));
    }

Called every Update, though same effect with FixedUpdate

#

and this is the ground ray:

 _groundRay = Physics2D.Raycast(this.transform.position, -Vector2.up, 0.2f, _groundMask);
#

if you need me to explain anything I will

#

oh and rb2d.gravityscale is 0

#

like the built in gravity

dull cradle
#

and the multiplier is 3

#

Snapping and Glitchy Movement when rotating player

#

@heady wasp sorry for pinging but are you checking this?

heady wasp
#

you shouldn't be directly modifying the Transform if you are using Rigidbody motion

dull cradle
#

how do I get the same effect though

#

the gradual change

heady wasp
#

rotate it through angular velocity or MoveRotation

dull cradle
#

rotate the rigidbody instead of transform?

heady wasp
#

yes...

#

you should not be modifying Transform directly when using Rigidbody motion

dull cradle
#

does MoveRotation set the rotation directly or does it order it to move

#

like velocity

#

lemme read the docs

heady wasp
#

it cues up the rotation to happen during the physics simulation

dull cradle
#

but it doesn't happen gradually, it happens instantly

heady wasp
#

so does your this.transform.eulerAngles = code currently

#

you just do it little by little each frame, as you currently are

dull cradle
#

actually hold on

#

100deg per second it says

#

right right

#

but

#

how do I make it stop at exactly the angle I need

#

the same way MoveTowardsAngle does

heady wasp
#

there's no reason to stop using MoveTowardsAngle

dull cradle
#

yes but I can't use MoveTowardsAngle with quaternions

#

because it's not a float

heady wasp
#

what?

#

Quaternion.Euler is a thing if you really must use euler angles

dull cradle
#

when using MoveTowardsAngle, it stops at that angle

heady wasp
#

you can also just do Quaternion.RotateTowards

dull cradle
#

oh rotate towards

#

doesn't that conflict with rigidbody movement?

#

same way transform does?

heady wasp
#

not if you're using it with MoveRotation

#

it simply calculates a Quaternion

#

it doesn't actually do anything

#

the actual rotation only happens one of two ways:

  • through the Transform
  • through the RIgidbody
#

it doesn't matter what math you use to calculate the rotation you want to apply

dull cradle
#

so MoveRotation(Quaternion.RotateTowards(angle))

#

lemme do some testing

heady wasp
#

RotateTowards doesn't take an angle

#

it takes a current rotation, a target rotation, and a max amount to rotate

dull cradle
#

it takes a quaternion as target rotation?

heady wasp
#

yes

dull cradle
#

and to get that quaternion

#

I use Quaternion.Euler

#

?

heady wasp
#

I wouldn't, but you could

dull cradle
#

how would you do it?

heady wasp
#

This is a 2d game right?

dull cradle
#

Yes

heady wasp
#

but you're using a 3d rigidbody?

dull cradle
#

no, RB2D

heady wasp
#

then you don't need to worry about quaternions at all

#

Rb2D uses a float for its rotation

dull cradle
#

so what do I do exactly?

heady wasp
#

I would just use Mathf.MoveTowards

dull cradle
#

but that's what I did

#

ohhhhh

#

I get you now

#

MoveRotation(Mathf.MoveTowardsAngle(the angle thingy))

#

right?

heady wasp
#

yes

dull cradle
#

I'll try this

#

but might need a -angle

heady wasp
dull cradle
#

it doesn't move at all

heady wasp
#

not transform.eulerAngles.z

dull cradle
#

I have z rotation locked

#

do I need to disable that?

#

this

#

@heady wasp moverotation doesn't work with Freeze Rotation on

#

and that's not good

#

I need to sleep, it's 1am

#

can we continue tomorrow?

dull cradle
#

@heady wasp hey

#

Not working well

#

it requires freeze rotation to be off

#

and that makes the player react to hitting his head on a block for example