void followPosition(IF_FollowEntity followEntity) { var sourceView = followEntity.FollowSourceEntity.GetGameObject(); var targetView = followEntity.FollowTargetEntity.GetGameObject(); if (sourceView == null || targetView == null) { return; } var transformToSource = followEntity.FollowSourceEntity.GetGameObject().transform; var transformToTarget = followEntity.FollowTargetEntity.GetGameObject().transform; Vector3 targetTransformPosition = transformToTarget.TransformPoint(followEntity.OffsetPosition); Vector3 newPosition; if (followEntity.SmoothPosition) { float alpha = Mathf.Clamp01(Vector3.Distance(followEntity.CalculatedPosition, targetTransformPosition) / followEntity.MaxDistanceDeltaPerFrame); newPosition = Vector3.Lerp(followEntity.CalculatedPosition, targetTransformPosition, alpha); } else { newPosition = targetTransformPosition; } followEntity.CalculatedPosition = newPosition; transformToSource.position = followEntity.CalculatedPosition; }
void followRotation(IF_FollowEntity followEntity) { var sourceView = followEntity.FollowSourceEntity.GetGameObject(); var targetView = followEntity.FollowTargetEntity.GetGameObject(); if (sourceView == null || targetView == null) { return; } var transformToSource = followEntity.FollowSourceEntity.GetGameObject().transform; var transformToTarget = followEntity.FollowTargetEntity.GetGameObject().transform; Quaternion targetTransformRotation = transformToTarget.rotation * Quaternion.Euler(followEntity.OffsetRotation); Quaternion newRotation; if (followEntity.SmoothRotation) { float alpha = Mathf.Clamp01(Quaternion.Angle(Quaternion.Euler(followEntity.CalculatedRotation), targetTransformRotation) / followEntity.MaxDistanceDeltaPerFrame); newRotation = Quaternion.Lerp(Quaternion.Euler(followEntity.CalculatedRotation), targetTransformRotation, alpha); } else { newRotation = targetTransformRotation; } followEntity.CalculatedRotation = newRotation.eulerAngles; transformToSource.rotation = Quaternion.Euler(followEntity.CalculatedRotation); }
void follow(IEntity entity, IF_FollowEntity followEntity) { if (entityDatabase.GetCollectionFor(followEntity.FollowSourceEntity) == null || entityDatabase.GetCollectionFor(followEntity.FollowTargetEntity) == null) { entityDatabase.RemoveEntity(entity); return; } if (followEntity.FollowPosition) { followPosition(followEntity); } if (followEntity.FollowRotation) { followRotation(followEntity); } }