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
0
Question by sokigb0128 · Jun 05, 2018 at 03:12 AM · 2d gamerigidbody2dcenterofmass

I can't change a value to Regidbody2D.centerOfMass

First, I'm not native english user. so I apologize for having an incorrect expression.

I'm developing 2D puzzle action game. There is an object to rotate by Rigidbody2D.MoveRotation in the game. I changed that object's Rigidbody2D.centerOfMass for changing center of rotation. However, when I check the log, the centerOfMass has not changed. I want to know how to solve this problem.

 void Update(){
         // Change centerOfMass
         rd2D.centerOfMass = new Vector2(100.0f, 100.0f); 
         Debug.Log ("Center =" + rd2D.centerOfMass);  //The result is "Center=(0.0,0.0)"
     }

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 Kciwsolb · Jun 05, 2018 at 01:57 PM

While I don't think you need to be updating centerOfMass every Update, this should work assuming "rd2D" is a valid Rigidbody2D.

I ran the following code in Unity and it behaves as expected:

 private Rigidbody2D rigidbody2D;
 
 private void Start ()
 {
     rigidbody2D = GetComponent<Rigidbody2D>();
     rigidbody2D.centerOfMass = new Vector2(100.0f, 100.0f);
     Debug.Log(rigidbody2D.centerOfMass); //Outputs: "(100.0, 100.0)"
 }

I am guessing there is something wrong with your "rd2D" variable. Try copying the above code, and see it it works properly. Maybe you could post the rest of your code so we can see if there is a problem elsewhere.

By the way, your English is better than many native English speakers. There's no need at all to apologize.

Comment
Add comment · Show 1 · 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 sokigb0128 · Jun 06, 2018 at 01:20 AM 0
Share

Thank you for answering. I tried copying your code,but it wasn't work. Tentatively, I will post the full code of the rotate object.

 public class test : $$anonymous$$onoBehaviour {
 
     Rigidbody2D rigidbody2D = new Rigidbody2D ();
     float rotfloat=0;
 
     // Use this for initialization
     void Start() {
 
         rigidbody2D= GetComponent<Rigidbody2D>();
         rigidbody2D.centerOf$$anonymous$$ass = new Vector2(100.0f, 100.0f);
 
         Debug.Log ("Center =" + rigidbody2D.centerOf$$anonymous$$ass); //Somehow (0.0,0.0) outputs.
     }
 
     void Update(){
         rotfloat += 0.5f;
         if (rotfloat > 180) {
             rotfloat = 0;}
 
         rigidbody2D.$$anonymous$$oveRotation(rotfloat);
     }
 
 
 }




avatar image
0

Answer by sokigb0128 · Jun 06, 2018 at 02:40 AM

solved!!!

Since "Rigidbody2D.centerOfMass" is a local coordinate with "transform.position" as the origin.
So I create new sprite in the object's script, and specify new sprite's pivot coordinate.
I still don't know the reason why the value does not change centerOfMass, but I could the processing I was looking for.
Thank you for your help!!

 private void Start() {

         Sprite nowSprite = GetComponent<SpriteRenderer>().sprite;
         Sprite newSprite =Sprite.Create (nowSprite.texture, nowSprite.rect, new Vector2 (3,3)); //texture,rect,Pivot
         GetComponent<SpriteRenderer> ().sprite = newSprite;
     }



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

95 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

Related Questions

How do I make Rigidbody2D movement smoother? 0 Answers

Player doesnt want to move from moving platform 0 Answers

How do i stop rigidbody2D bounce 1 Answer

Instantiate new gameobjects on collision, keeping velocity, up to a cap 1 Answer

Hi,How do I make a 2D laser beam with stable speed ? 1 Answer


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