简体   繁体   中英

Setting Rigidbody2D body type to "static" in code?

I recently updated my Unity (to 5.5.0f3 from 5.4.1f1), and now I seem to have some problems with a Rigidbody2D in one of my games.

So basically I used to use this code to make my gameobject (player) non-intractable and taken out of the physics controls:

Player.GetComponent<Rigidbody2D>().isKinematic = true;

This is not working properly after the update of Unity, and upon setting the player to kinematic it keeps moving in the same direction as when it was "dynamic" (but uncontrollable). Note: this was not the case in Unity 5.4.1f1.

So I went into the Rigidbody2D component and noticed that it had changed. How my old Rigidbody2D looked like and How my new RigidBody2D looks like now

The "kinematic" option was moved into a "body type" option, and in runtime it does change the body type to Kinematic using the ".isKinematic = true" string (but it's not working properly as mentioned before).

So I tried changing the body type value to "static" manually doing runetime and noticed that worked perfectly!

So my question is: How do i change the body type to static in the code? (like the "isKinematic"), and what happened in the changes to Rigidbody2D (and Kinematics?)

The bodyType property on Rigidbody is probably what you're looking for:

Player.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;

It's worth mentioning that something else might be wrong if updating between minor versions of Unity is what broke it, but I wouldn't know for sure without seeing more of your project.

I am facing a problem in this same context, where in playerDeath Script i have added

rb.bodyType = RigidbodyType2D.Static;

after restart level, getting lots of warning "Cannot use 'velocity' on a static body"

How to resolve this warning? as this causes lag in the game.

I tried making it dynamic in restartlevel method, warning still exists.

Please suggest!

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM