public static void FollowTarget(AttachObject target, AttachObject follower, AttachStrategyFollowPositionOptions positionOptions, AttachStrategyFollowRotationOptions rotationOptions) { if (rotationOptions.behavior != RotationBehavior.DoNothing) { Quaternion desiredRotation = FollowingUtility.GetDesiredRotationForObject(follower, rotationOptions.behavior, rotationOptions.GetRotationParamSelector(), rotationOptions.GetLookAtPositionParamSelector(), rotationOptions.GetUpParamSelector(), rotationOptions.GetForwardParamSelector(), rotationOptions.GetOffsetParamSelector()); Quaternion nextRotation; if (rotationOptions.speed == 0) { nextRotation = desiredRotation; } else { nextRotation = Quaternion.RotateTowards(follower.attachable.transform.rotation, desiredRotation, Time.deltaTime * rotationOptions.speed); } ClingyUtils.MoveRotation(nextRotation, follower.attachable.transform, rotationOptions.rotateMethod, follower.rigidbody, follower.rigidbody2D); } // todo - move before rotate, rotate before move...it's situationally dependent if (positionOptions.behavior != PositionBehavior.DoNothing) { FollowingObjectState state = GetObjectState(follower); Vector3 desiredPos = FollowingUtility.GetDesiredPositionForObject(follower, positionOptions.behavior, positionOptions.GetFollowerAnchorParamSelector(), positionOptions.GetTargetAnchorParamSelector()); if (!positionOptions.followX) { desiredPos.x = follower.attachable.transform.position.x; } if (!positionOptions.followY) { desiredPos.y = follower.attachable.transform.position.y; } if (!positionOptions.followZ) { desiredPos.z = follower.attachable.transform.position.z; } Vector3 nextPos; if (positionOptions.smoothing == 0) { nextPos = desiredPos; } else { nextPos = Vector3.SmoothDamp(follower.attachable.transform.position, desiredPos, ref state.velocity, positionOptions.smoothing); } ClingyUtils.MovePosition(nextPos, follower.attachable.transform, positionOptions.moveMethod, follower.rigidbody, follower.rigidbody2D); } // // note this sets localScale to lossyScale, so only works if there's no parent scale on the attacher // if (followScale) // context.attacher.transform.localScale = context.receiver.transform.lossyScale; }
// returns a world rotation public static Quaternion GetLookAt2DRotation(GameObject gameObjectToRotate, Attachment attachment, ParamSelector lookAtPos, AttachObject reference = null, bool flipX = false) { Vector3 lookAtWorldPos = lookAtPos.GetWorldPosition(attachment, reference); if (gameObjectToRotate.transform.position == lookAtWorldPos) { return(gameObjectToRotate.transform.rotation); } return(ClingyUtils.LookAt2D(gameObjectToRotate.transform.position, lookAtWorldPos, flipX)); }