Hi coders,
I have a character (yellow in the picture) who orbits a tiny sphere (blue) while holding the space bar. I’m trying to make him fly off in the appropriate direction when releasing the space bar. lastTarget.transform.position - transform.position works okay, but it doesn’t really feel natural, given the characters direction of orbit.
On the screen shot, I’ve marked the direction the character will fly if the space bar were to be released at that given phase of the orbit. I’ve also marked the direction I would prefer the character to fly.
Here’s the code I’m using to add force. Any ideas would be much appreciated. Thanks!
if (Input.GetKeyUp(“space”)) {
meter.localScale.x = 0;
if (currentPlanet != null) {
orbitPhase = 0;
lastTarget = currentPlanet;
currentPlanet = null;
var dir = lastTarget.transform.position - transform.position;
//Debug.Log(dir);
dir = dir.normalized;
rigidbody2D.AddForce(-dir * Mathf.Clamp(slingForce,-2500,2500));
slingForce = startSlingForce;
}
}