- Home /
i have rotated an gameobject but the axis of the object did not change
I have tried to make a planet and the player is supposed to walk around the planet while rotating itself so that the leg of the player touches the ground as shown in the picture below. however when my player rotate around the planet, the axis of the player does not change this has caused a problem whereas when i am at the bottom of the planet i will be unable to make my player jump anymore, anyone know how to fix this problem?
Answer by megabrobro · Jun 13, 2018 at 12:38 PM
i may be wrong, but you are perhaps rotating the 'image' or texture part of the gameobject rather than the object transform itself.
Could you post the code you are using to make the things rotate?
sure, the following code is what i gave to the planet so that it attracts and rotate the player public class GravityAttractor : $$anonymous$$onoBehaviour { public float gravity = -10f; public Rigidbody2D rb; private void Start( ) {
}
public void Attract(Transform body)
{
Vector3 targetDir = (body.position - transform.position).normalized;
Vector3 bodyUp = body.up;
rb = GameObject.FindGameObjectWithTag("Player").GetComponent<GravityBody>().rb;
body.localRotation = Quaternion.FromToRotation(bodyUp, targetDir) * body.localRotation;
rb.AddForce(targetDir * gravity);
}
}
sorry for the misalignment in the codes btw, this is my first time asking question here
@tokinom what is the Player's code? share it too.
public class GravityBody : $$anonymous$$onoBehaviour {
public Rigidbody2D rb;
GravityAttractor planet;
// Use this for initialization
void Awake () {
planet = GameObject.FindGameObjectWithTag("Planet").GetComponent<GravityAttractor>();
rb = GameObject.FindGameObjectWithTag("Planet").GetComponent<GravityAttractor>().rb;
}
// Update is called once per frame
void FixedUpdate () {
planet.Attract(transform);
}
}
Answer by bakir-omarov · Jun 13, 2018 at 03:55 PM
It is the dumbest (incorrect/inefficient) code for making circular gravity/2d planet gravity/rotate around planet/jump around planet/walk around 2d planet and etc...It doesn't work good, but you can try to start with this:
public class GravityBody : MonoBehaviour
{
public float gravitySize = 1f;
public Transform planet;
private Rigidbody2D m_rigidBody2D;
private void Start()
{
m_rigidBody2D = GetComponent<Rigidbody2D>();
m_rigidBody2D.gravityScale = 0;
m_rigidBody2D.isKinematic = false;
}
private void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector2 inputVector = new Vector2(h, v);
// for jumping
m_rigidBody2D.AddForce(inputVector);
Vector2 distanceVector = (planet.transform.position - transform.position).normalized;
m_rigidBody2D.AddForce(distanceVector * gravitySize);
float rot_z = Mathf.Atan2(distanceVector.y, distanceVector.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0f, 0f, rot_z-270);
}
}
Attach this code to your player (with Rigidbody2D and collider), drag and drop your circle (planet) to planet holder, and add collider to your planet.
Your answer
Follow this Question
Related Questions
2D Rotate towards GameObject acting odd. 2 Answers
Rotate Rigidbody 2D towards velocity 1 Answer
Rotate player (rigidbody) towards his movement 2 Answers
Pinball Flipper using Quaternion.Lerp? 1 Answer
Rotate with raw gyro data. 0 Answers