- Home /
The question is answered, right answer was accepted
The object doesn't line up.When it changes the object that is rotating around.
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);
}
}
}
Answer by Ermiq · Dec 01, 2019 at 10:09 AM
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'
}
}
It doesn't work but it is fine because i didn't give you much information.But don't worry i came up with a solution.Anyway i will not ask more questions here because i can't explane things well.Sorry for wasting your time.
Hey, don't say that. Feel free to ask your questions anytime. Of course people sometimes don't understand what's the problem actually or they might understand it in wrong way, but it's totally fine. We're just people, not mashines. And it wasn't a waste of time. I come to this place for fun and I get fun by trying to solve code puzzles. Successful solutions are more fun, that's for sure, but anyway if it wasn't fun to me, I wouldn't try to help you. Glad you've found the solution, btw.
Follow this Question
Related Questions
Tetris transform.rotate messes up childrens position? 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Issue rotating objects with input from the mouse wheel 1 Answer
How to make a tick speed for heart health system?,How to make a tick speed for health script? 1 Answer