Skip to content

Instantly share code, notes, and snippets.

@Apsu
Last active March 26, 2020 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Apsu/a303ea1e4cd5c834db9f29a5fe7546b8 to your computer and use it in GitHub Desktop.
Save Apsu/a303ea1e4cd5c834db9f29a5fe7546b8 to your computer and use it in GitHub Desktop.
Warping

Warping

Keep track of

  • Camera look direction
    • Use to calculate relative rotation to transform.rotation
  • Move direction
    • Use to calculate relative rotation to transform.forward

OnAnimatorIK

Stride

  • Set foot IK goals and weights
    • Get goal positions
    • Add/subtract in forward direction based on speed

Look

  • Use look direction to get look position
    • lookDirection = mainCam.transform.forward
    • lookDirection.y = 0f
      • Clamp to XZ plane
    • lookPosition = transform.position + lookDirection
  • SetLookAtPosition with look position
    • Rotates upper body

OnAnimatorMove

Movement

controller.Move(transform.position + transform.forward * speed * moveRate);

Rotation

  • moveRotation = Quaternion.AngleAxis(Vector3.SignedAngle(transform.forward, moveDirection, transform.up), transform.up)

  • Get difference between move direction and look direction

    • difference = Vector3.SignedAngle(moveDirection, lookDirection, transform.up)
    • If difference > or < than max/-max
      • Add difference to move rotation
        • moveRotation *= Quaternion.AngleAxis(difference - max or + max, transform.up)
      • Set animator float for rotation
        • Triggers turn in place animation if idle
        • Triggers lean if moving
  • Rotate transform to move rotation

    • transform.rotation *= moveRotation
    • Will be 0 rotation when look direction is inside max range and transform forward = move direction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment