# Spherical Pathfinding?

Discussion in 'Scripting' started by MoyaAdam, Feb 24, 2014.

Joined:
Jul 24, 2010
Posts:
50
So I've done some searching and I've come close to what I want, but I can't quite get it. So let me start from scratch.
Take this image here:

Where the blue pins represent the Polar opposites of the sphere (Y+ and Y-).

Now lets add our player (the orange tack)

And finally our enemy, the green tack.

I want the green tack to be able to look "at" the player and move towards him around the sphere, and to this I know that I would need to draw a diameter of the sphere that intersects the 2 points, then find the shorter distance around that sphere to get my direction, then face that direction.

Now I found this link here:

which lead me to the Haversine formula, and a nice Unity3d example of it.

Rawsteel mentions that the Vector2's that there are inputed into it, I'm assuming that the x and y values are the azimuth angle and the inclination angle mentioned in the Wiki article, however I don't know if that's correct. I'm having a hard time understanding the Haversine formula, but it seems to follow the same concept that I'm going for.

If anyone could help me implement this into Unity3D you will have my ever-lasting nerd love.

P.S. I want to avoid using the A* path finding system or anything similar, since this will add many files to my project, and there will be no objects for the enemy to navigate around.

Last edited: Feb 24, 2014
2. ### RvBGames

Joined:
Oct 22, 2013
Posts:
141

Joined:
Jul 24, 2010
Posts:
50
Slerp will help later with smoothing out some rotations, however I did get it workings with this simple code:

Code (csharp):
1. void Update () {
2.         float yDist = Vector3.Distance(transform.position,Player.transform.position)*Mathf.Sin ((Vector3.Angle (transform.up,(Player.transform.position - transform.position).normalized) - 90) * Mathf.Deg2Rad);
3.         mLookAtPos = Player.transform.position + transform.up * yDist;
4.     }
I guess all my math and planning was wrong due to some simple trig, however now I have the issue of how to actually look at that mLookatPos. If I use the simple transform.Lookat function I get a bunch of weird angles. So I need to do one that only affects the transform.up, but I can't think of a way. Any help?

Zarbuz likes this.
4. ### NivekJump

Joined:
Oct 9, 2014
Posts:
2
Hi Friend.
This post was great!!
You help me to understand a lot the spherical movement and to develop my AI, so thankyou a lot!
I dont know if you already solve your problem, but you can apply the LooAt function to affect the transform.up.
Just add another parameter to the function, something like this.
transform.LooAt(mLookAtPos, transform.up);

Worked for my, so thank you again

unityunity