private void FixedUpdate() { Vector2 movement = input.GetCircularMovementVector(inputDeadZone); bool isFacingMovementDirection = true; Vector2 targetVelocity; // Change rotation if (faceMovementDirection && movement != Vector2.zero) { float rotationTarget = Mathf.Atan2(movement.y, movement.x) * Mathf.Rad2Deg + rotationOffset; float rotationDelta = Mathv.ClampAngle180(rotationTarget - transform.eulerAngles.z); isFacingMovementDirection = Mathf.Abs(rotationDelta) <= turnSpeed; rotationDelta = (rotationDelta > 0) ? Mathf.Min(rotationDelta, turnSpeed) : Mathf.Max(rotationDelta, -turnSpeed); transform.Rotate(rotationDelta * Vector3.forward); } // Change velocity float tq = Mathv.LerpQRound(0, 1, Mathf.InverseLerp(inputDeadZone, 1, movement.magnitude), walkSpeedLevels); if (onlyMoveForward) { if (isFacingMovementDirection) { targetVelocity = Mathf.Lerp(minWalkableSpeed, walkSpeed, tq) * movement.normalized; } else { targetVelocity = minWalkableSpeed * transform.TransformDirection(Quaternion.AngleAxis(rotationOffset, Vector3.forward) * Vector3.right); } } else { targetVelocity = Mathf.Lerp(minWalkableSpeed, walkSpeed, tq) * movement.normalized; } if (acceleration == Mathf.Infinity) { rb.velocity = targetVelocity; } else { rb.AddForce(acceleration * targetVelocity); if (targetVelocity.magnitude == 0) { rb.drag = deceleration; } else { float idealDrag = acceleration / targetVelocity.magnitude; rb.drag = acceleration / (acceleration * Time.deltaTime + 1); // [TODO] work around PhysX drag approximation } } UpdateAnimation(); }
private void FixedUpdate() { Vector2 movement = input.GetCircularAimVector(inputDeadZone); if (movement != Vector2.zero) { float rotationTarget = Mathf.Atan2(movement.y, movement.x) * Mathf.Rad2Deg - rotationOffset; float rotationDelta = Mathv.ClampAngle180(rotationTarget - transform.eulerAngles.z); rotationDelta = (rotationDelta > 0) ? Mathf.Min(rotationDelta, turnSpeed) : Mathf.Max(rotationDelta, -turnSpeed); transform.Rotate(rotationDelta * Vector3.forward); } }