- Home /
Click to move on a sphere (planetary gravity)
Can someone help me create a click to move system on a sphere? I use planetary gravity for my game, and i've tryed different ways and tought about different ways. First thing I tought about was making waypoints between position A and B, this way the line would not be straight but curved around the sphere. Even tough I don't know how to do this, maybe some other people now how to do this?
Excuse me for my English. - Wolftic
You have made life far more difficult for yourself by picking a spherical landmass... You could(might have to) do something like this(I've used very long names to explain what is happening...) :
function sub$$anonymous$$ove_To_Point_A_With_Physics__Called_Each_Fixed_Update() {
Your_Current_Position_To_PointA_Direction_Vector = PointA.transform.position - transform.position;
Vector_Tangent = Vector3.Cross(Your_Current_Position_To_PointA_Direction_Vector , Your_Current_Gravity_Vector_In_Relation_To_Your_Planets_Center);
Your_$$anonymous$$ove_Direction_Vector = Vector3.Cross(Your_Current_Gravity_Vector_In_Relation_To_Your_Planets_Center, Vector_Tangent);
Your_$$anonymous$$ove_Direction_Vector_Normalized = Your_$$anonymous$$ove_Direction_Vector * (1.0 / Your_$$anonymous$$ove_Direction_Vector.magnitude);
//Apply $$anonymous$$ovement Force in Your_$$anonymous$$ove_Direction_Vector_Normalized.
rigidbody.AddForce(Your_$$anonymous$$ove_Direction_Vector_Normalized * fFollow_Force, Force$$anonymous$$ode.Force);
}
Obviously you will have to declare all these vector3 vars somewhere... :)
Also note that this method will go nuts if your player object is on the exact opposite side of the world as the point A position, make sure you design in a way that makes that never happen...
(Edit, mixed things up a little, should be right now... :S)
You could try @$$anonymous$$rSoad 's method, and you can get your current gravity vector by gravityVector = (planetPos - yourPos); gravityVector.Normalize(); gravityVector *= gravityForceScalar;
Take a look at Great-Circle Navigation (wikipedia link: http://en.wikipedia.org/wiki/Great-circle_navigation)
It is the method of finding the most efficient "straight-line" path on a sphere.
You dont have to normalize it twice. Remove the extra moveDirectionNormalized = moveDirection * (1.0f / moveDirection.magnitude);
since you already do it on the previous line.