Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by tokinom · Jun 13, 2018 at 12:23 PM · rotationquaternionrigidbody2d

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.alt text however when my player rotate around the planet, the axis of the player does not changealt text 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?

help.jpg (14.9 kB)
help2.jpg (10.1 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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?

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tokinom · Jun 13, 2018 at 12:43 PM 1
Share

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);
     }
 }
 
avatar image tokinom · Jun 13, 2018 at 12:44 PM 0
Share

sorry for the misalignment in the codes btw, this is my first time asking question here

avatar image bakir-omarov tokinom · Jun 13, 2018 at 12:59 PM 0
Share

@tokinom what is the Player's code? share it too.

avatar image tokinom bakir-omarov · Jun 13, 2018 at 01:14 PM 1
Share
 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);
         
     }
 }
Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

121 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges