Unity: How to navigate between two points on a sphere?

Asked

Viewed 417 times

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.

inserir a descrição da imagem aqui

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.

  • 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)

1 answer

3


The solution is to use spherical coordinates. This coordinate system positions you on a sphere using the parameters radius, Phi angle and theta angle. While the Cartesian system, used by Unity, positions you in cubic format with parameters x, y and z.

Do the following:

1) Convert player and object position to spherical coordinates, through the following functions:

cartesiana para esferica

2) Increment or decrease the theta and Phi angles to bring the player object closer together.

3) Convert back to cartesian coordinates (x,y,z) and move the object to the new coordinates, using the following functions:

x

y

z

r is the radius of the sphere. In general it is a parameter of the spherical coordinate system, but if you are going to remain on the surface always, you can fix it.

x, y and z are the coordinates of the Cartesian system.

Phi and theta are the spherical system coordinates you will use.

Source: http://fma.if.usp.br/~Fleming/diffeo/node4.html https://en.wikipedia.org/wiki/Spherical_coordinate_system

  • It should also be noted that r is positive, theta is between 0 and 2*PI and Phi between 0 and IP.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.