2
I need to create a navigation system between two points in a sphere, the problem is that as the Unity Navmesh does not work with spherical objects I have to calculate and manually position the object to its destination.
I created a logic that may not work, where calculating the distance between the object and the destination plus the distance to the center would give me the position of the object, and reducing some percentage of the distance to the player I would have a new position to move the object, but I could not go further, because I can not convert this calculation to Vector3 for numerous problems I found.
Which correct way to do this? whereas I can’t use Navmesh, and that the navigation from the object to the player has to be gradual.
Below is an image that represents the logic (Maybe nonsense, rs) that I thought might work, and a script that I started.
public class NavMeshController : MonoBehaviour
{
[Header("Settings")]
public GameObject toPoint; // Ponto de destino
public PlanetController sphere;
private Renderer renderer;
private float radious;
private float center;
void Start ()
{
renderer = sphere.GetComponent<Renderer>();
radious = getRadious();
}
void Update ()
{
// transform.Translate(toPoint.transform.position * Time.deltaTime, Space.World);
}
float distanceToPoint()
{
return Vector3.Distance(transform.position, toPoint.transform.position);
}
float distanceToCenter()
{
Vector3 center = renderer.bounds.center;
return Vector3.Distance(transform.position, renderer.bounds.center);
}
float getRadious()
{
SphereCollider sphereCollider = sphere.GetComponent<SphereCollider>();
return Mathf.Max(sphereCollider.transform.lossyScale.x, sphereCollider.transform.lossyScale.x, sphereCollider.transform.lossyScale.x) * sphereCollider.radius;
}
}
Think on the other hand: walking on a sphere can be the same as "rotating" on its central axis. You can simply put the pivot of the character in the center, "climb up" it until it’s outside the sphere, and just tinker with the angles. If you need to do things using distances, etc., or pathing, then you’ll need to do geodetic calculations.
– Bacco
Another detail, if your object can follow the player, just leave the ball with Rigidbody, so the object is obliged to walk over the ball and not through it! (This in case I have understood your question)
– Junior Moreira