This is in C#.I have a Player gameobject with a 2dboxcolider and planets with a 2dcircle colider and the player can rotate around the planets with A and D.By pressing space he will go in the direction that is rotate.But sometimes the player does’t line up.This is how it shold look 
but some times it looks like this
i tride a lot of things but none of them worked so i diceded to write this.
here is the code(Only the player has a script):
public class Player : MonoBehaviour {
public bool NaPlaneti,Invert;
private GameObject P;
public Rigidbody2D rb;
public int BrzinaLetenja,BrzinaSetanja;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
if (NaPlaneti == false && Invert == false) rb.velocity = transform.up * BrzinaLetenja * Time.deltaTime;
if (NaPlaneti == false && Invert == true) rb.velocity = (transform.up * BrzinaLetenja * Time.deltaTime) * -1;
if ((Input.GetKey("a") || Input.GetKey(KeyCode.LeftArrow)) && NaPlaneti == true) rb.transform.RotateAround(P.transform.position, new Vector3(0, 0, 1), (1 * Time.deltaTime) * BrzinaSetanja);
if ((Input.GetKey("d") || Input.GetKey(KeyCode.RightArrow)) && NaPlaneti == true) rb.transform.RotateAround(P.transform.position, new Vector3(0, 0, 1), (-1 * Time.deltaTime) * BrzinaSetanja);
if (Input.GetKeyDown(KeyCode.Space) && NaPlaneti == true) {
NaPlaneti = false;
if (Invert == true) Invert = false;
else if (Invert == false) Invert = true;
}
}
void OnTriggerEnter2D(Collider2D other) {
if (other.name.Substring(0, 6) == "Planet") {
NaPlaneti = true;
P = other.gameObject;
rb.velocity = new Vector2(0,0);
transform.localScale = new Vector2(-transform.localScale.x,-transform.localScale.y);
transform.RotateAround(Vector3.zero, Vector3.up, 20 * Time.deltaTime);
}
}
}
Ermiq
2
Looks like this line is wrong:
transform.RotateAround(Vector3.zero, Vector3.up, 20 * Time.deltaTime);
Try the following code. It’s the same as yours but I’ve edited it so it doesn’t have unnecessary stuff and it’s simpler to read. But I’ve changed the problematical line with the new method where I get the angle to try to get the player lined up correctly:
public class Player : MonoBehaviour {
public bool NaPlaneti,Invert;
private GameObject P;
public Rigidbody2D rb;
public int BrzinaLetenja,BrzinaSetanja;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
if (!NaPlaneti) {
rb.velocity = transform.up * BrzinaLetenja * Time.deltaTime;
if (Invert) {
rb.velocity = -rb.velocity;
}
}
if (NaPlaneti) {
if (Input.GetKey("a") || Input.GetKey(KeyCode.LeftArrow)) {
rb.transform.RotateAround(P.transform.position, new Vector3(0, 0, 1), (1 * Time.deltaTime) * BrzinaSetanja);
}
if (Input.GetKey("d") || Input.GetKey(KeyCode.RightArrow)) {
rb.transform.RotateAround(P.transform.position, new Vector3(0, 0, 1), (-1 * Time.deltaTime) * BrzinaSetanja);
}
if (Input.GetKeyDown(KeyCode.Space)) {
NaPlaneti = false;
Invert = !Invert;
}
}
}
void OnTriggerEnter2D(Collider2D other) {
if (other.name.Substring(0, 6) == "Planet") {
NaPlaneti = true;
P = other.gameObject;
rb.velocity = new Vector2(0,0);
transform.localScale = new Vector2(-transform.localScale.x,-transform.localScale.y);
// Seems like your problem is in this line
// transform.RotateAround(Vector3.zero, Vector3.up, 20 * Time.deltaTime);
// try out another method
LineUp();
}
}
void LineUp() {
// get the direction vector which is lined straight up from planet center through the player
Vector3 directionFromPlanet = transform.position - P.transform.position;
// get the angle between that direction line and the current player up vector
// the angle will be either between 0 and 180 or 0 and -180.
float angleToRotateAround = Vector2.SignedAngle(directionFromPlanet, transform.up);
// note that we assume here that your player's legs are closer to the planet, and his head is farther.
// you've flipped the player by inverting his scale, so it should be fine
// let's use player bottom as a pivot and rotate him around Z axis by the angle we've got
// I use Z axis because it looks like you have X as horizontal and Y as up. Also, you've used Vector3(0,0,1) to rotate player around planets, and that's the Z vector, the same as Vector3.forward
transform.RotateAround(transform.position, Vector3.forward, angleToRotateAround);
// here the player could be rotated opposite the direction you desire, and he will be perpendicular to the planet, if so, than change the 'angleToRotate' to '-angleToRotate'
}
}