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

2

I need to create a navigation system between two points on a sphere, the problem is that since Unity's NavMesh does not work with spherical objects I have to calculate and manually position the object to its destination.

I created a logic that might not work, where calculating between the distance of 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 would have a new position for move the object, but I could not go any further, because I can not convert this calculation to Vector3 for numerous problems I encountered.

Which way to do this? considering that I can not use Navmesh, and that the navigation of the object to the player has to be gradual.

Below is an image that represents the logic (Maybe nonsense, rs) that I imagined could work, and a script I started.

publicclassNavMeshController: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;
    }
}
    
asked by anonymous 29.12.2015 / 22:20

1 answer

3

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

Do the following:

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

2)Increaseordecreasethetaandphianglestobringtheobjectclosertotheplayer.

3)ConvertbacktoCartesiancoordinates(x,y,z)andmovetheobjecttothenewcoordinatesusingthefollowingfunctions:

ristheradiusofthesphere.Ingeneralitisaparameterofthesphericalcoordinatesystem,butifyouaretoremainonthesurfacealways,youcanfixit.

x,yandzarethecoordinatesoftheCartesiansystem.

phiandthetaarethecoordinatesofthesphericalsystemyouwilluse.

Source: link link

    
29.12.2015 / 23:14