- Home /
How to use centerOfMass of rigidbody properly??
Hi guys!...I am new to unity and am currently working on a 2d game...it's a shooting game where my character can move both ways(left and right),jump,and shoot......the player object has a box collider attached with it...the problem is when i move the character around,sometimes(actually often) due to some sort of inertia effect the character seems to lose its balance and fall on the ground.....like if it has a rectangular collider attached with it..it simply lies down on the ground.....I need to make the player more stable....so I thought if i changed it's centre of mass and brought it closer to the ground or feet of the character..that might solve the problem...I wrote a part in the script to change its centre of mass like this....
rb2d=GetComponent<Rigidbody2D> ();
rb2d.centerOfMass=transform.Find("Floor_position").position;
here "Floor_position" is an empty child game object of the player game object and it determines the new centre of mass of the player... the problem is whenever i run the game....some invisible force seems to carry the player off the ground and place it in a different position......and the rectangular box collider attached with the player simply lies down from the very beginning.....need help....thanks in advance.....if i am not clear enough...i can post pictures of the situation in more detail....
Answer by Marioooo · Dec 09, 2018 at 03:07 PM
if you are making a character that must keep standed, on the rigidbody you attached, you must chang the value of the option called "angular drag" to Zero. with that option, the object will never rotate unles you do it on code, if wou want a low rotation, just use a very low value, like 0.01 Good Luck!
Answer by Nafis1053 · Dec 09, 2018 at 03:30 PM
@Marioooo Thanks for ur reply....i have done what you have told...but i think my problem was a bit different all along...decreasing the angular drag makes it hard to rotate...but i need my character to stay standing /alligned at all times...when it falls from an edge in the game, or i start moving it while it was standing in an inclined place...it starts to rotate again.
in that case, you maybe should create your own physics system... but with this approach you will need to use a capsule collider to climb up an inclinated place..
this is a official tutorial, check it out to create yor own physics system.. Good Luck and $$anonymous$$eep Creating!