Skip to content

Instantly share code, notes, and snippets.

@mstfmrt07
Last active June 19, 2021 14:03
Show Gist options
  • Save mstfmrt07/6da0ad1e7fdd3618af0dc4527757e577 to your computer and use it in GitHub Desktop.
Save mstfmrt07/6da0ad1e7fdd3618af0dc4527757e577 to your computer and use it in GitHub Desktop.
public Transform ghostPlayer;
private IEnumerator RollToDirection(Direction swipeDirection)
{
if (!isRolling)
{
isRolling = true;
float angle = 90f;
Vector3 axis = GetAxis(swipeDirection);
Vector3 directionVector = GetDirectionVector(swipeDirection);
Vector2 pivotOffset = GetPivotOffset(swipeDirection);
pivot.position = transform.position + (directionVector * pivotOffset.x) + (Vector3.down * pivotOffset.y);
//simulate before the action in order to get an ideal result
CopyTransformData(transform, ghostPlayer);
ghostPlayer.RotateAround(pivot.position, axis, angle);
float elapsedTime = 0f;
while (elapsedTime < rollDuration)
{
elapsedTime += Time.deltaTime;
transform.RotateAround(pivot.position, axis, (angle * (Time.deltaTime / rollDuration)));
yield return null;
}
CopyTransformData(ghostPlayer, transform);
isRolling = false;
}
}
public void CopyTransformData(Transform source, Transform target)
{
target.localPosition = source.localPosition;
target.localEulerAngles = source.localEulerAngles;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment