#Snapping and Glitchy Movement when rotating player
1 messages · Page 1 of 1 (latest)
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
I used this instead
and the multiplier is 3
Snapping and Glitchy Movement when rotating player
@heady wasp sorry for pinging but are you checking this?
you shouldn't be directly modifying the Transform if you are using Rigidbody motion
what should I do to rotate? Quaternions?
how do I get the same effect though
the gradual change
The rigidbody
rotate it through angular velocity or MoveRotation
rotate the rigidbody instead of transform?
does MoveRotation set the rotation directly or does it order it to move
like velocity
lemme read the docs
it cues up the rotation to happen during the physics simulation
but it doesn't happen gradually, it happens instantly
so does your this.transform.eulerAngles = code currently
you just do it little by little each frame, as you currently are
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
there's no reason to stop using MoveTowardsAngle
when using MoveTowardsAngle, it stops at that angle
you can also just do Quaternion.RotateTowards
oh rotate towards
doesn't that conflict with rigidbody movement?
same way transform does?
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
RotateTowards doesn't take an angle
it takes a current rotation, a target rotation, and a max amount to rotate
it takes a quaternion as target rotation?
yes
I wouldn't, but you could
how would you do it?
This is a 2d game right?
Yes
but you're using a 3d rigidbody?
no, RB2D
then you don't need to worry about quaternions at all
Rb2D uses a float for its rotation
so what do I do exactly?
I would just use Mathf.MoveTowards
but that's what I did
ohhhhh
I get you now
MoveRotation(Mathf.MoveTowardsAngle(the angle thingy))
right?
yes
just use Rb2D.rotation
it doesn't move at all
not transform.eulerAngles.z