Flipping a capsule collider along with animation in Unity
I had this weird problem, when I flipped an animation, the capsule collider, which was also animated, was jumping all over the place.
Not much help on the forums, but this video really helped:
void FixedUpdate()
{
//if moving right
if (flyDirection == FlyDirection.right)
{
// sr.flipX = true;
transform.eulerAngles = new Vector3(0, 180, 0);
if (Time.time - lastDirectionChangeTime < moveTime)
{
rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
}
else //change directions
{
lastDirectionChangeTime = Time.time;
flyDirection = FlyDirection.left;
}
}
else if (flyDirection == FlyDirection.left)
{
// sr.flipX = false;
transform.eulerAngles = new Vector3(0, 0, 0);
if (Time.time - lastDirectionChangeTime < moveTime)
{
rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
}
else //change directions
{
lastDirectionChangeTime = Time.time;
flyDirection = FlyDirection.right;
}
}
}
Comments
Post a Comment