- Home /
Question by
Flowmotions · Nov 10, 2018 at 06:59 PM ·
movementtransformtranslate
How to find out magnitude of translation
I’ve got the planet and some little spheres around this planet, when scene starts, these spheres should be at the same distance from planet, how can I get that? I can’t find out magnitude of sphere’s translation
Comment
Best Answer
Answer by CraigGraff · Nov 10, 2018 at 07:36 PM
/// <summary>
/// Get the current distance between a planet and a sphere.
/// </summary>
/// <param name="planet">The position of planet the sphere is orbiting.</param>
/// <param name="sphere">The position of a sphere orbiting the planet.</param>
/// <returns></returns>
float GetTranslationMagnitude(Vector3 planet, Vector3 sphere)
{
Vector3 offset = planet - sphere;
return Vector3.Magnitude(offset);
}
/// <summary>
/// Set the distance between a planet and a sphere.
/// </summary>
/// <param name="planet">The planet the sphere is orbiting.</param>
/// <param name="sphere">A sphere orbiting the planet.</param>
/// <returns></returns>
void SetTranslationMagnitude(Transform planet, Transform sphere, float distance)
{
Vector3 offset = planet.position - sphere.position;
sphere.position = planet.position + offset.normalized * distance;
}