- Home /
Rigidbody is behaving abnormally when transformed manually
In my game player is a rigidbody. He can control other characters by being inside it. The characters are also rigidbodies.
So for that when capturing a character i am setting player's parent as that character and transforming its position, scale, rotation.
public function Capture() {
(player.GetComponent("RigidbodyFPSController") as RigidbodyFPSController).enabled = false;
(player.GetComponent("MouseLook") as MouseLook).enabled = false;
playerFlashLightState = playerFlashLight.light.enabled;
playerFlashLight.light.enabled = false;
player.rigidbody.isKinematic = true;
Physics.IgnoreCollision(player.collider, collider, true);
player.rigidbody.Sleep();
player.transform.SetParent(eye.transform, false);
player.transform.localPosition = Vector3(0, 0, 0);
player.transform.localEulerAngles = Vector3(0, 0, 0);
player.transform.localScale = Vector3(0.01, 0.01, 0.01);
rigidbody.isKinematic = false;
(GetComponent("RigidbodyFPSController") as RigidbodyFPSController).enabled = true;
(GetComponent("MouseLook") as MouseLook).enabled = true;
ScreenFader.FadeInScreen();
animator.SetBool("Captured", true);
captured = true;
}
And when releasing it i am reverting everything again
public function Release() {
player.transform.SetParent(null, true);
player.transform.Translate(0, player.collider.bounds.size.y/2 + collider.bounds.size.y, 0);
player.transform.eulerAngles = Vector3(0, player.transform.eulerAngles.y, 0);
player.transform.localScale = Vector3(1, 1, 1);
player.rigidbody.isKinematic = false;
Physics.IgnoreCollision(player.collider, collider, false);
player.rigidbody.WakeUp();
(player.GetComponent("RigidbodyFPSController") as RigidbodyFPSController).enabled = true;
(player.GetComponent("MouseLook") as MouseLook).enabled = true;
playerFlashLight.light.enabled = playerFlashLightState;
rigidbody.isKinematic = true;
(GetComponent("RigidbodyFPSController") as RigidbodyFPSController).enabled = false;
(GetComponent("MouseLook") as MouseLook).enabled = false;
ScreenFader.FadeInScreen();
animator.SetBool("Captured", false);
captured = false;
}
Above codes are attached to the controllable characters.
But after this, the player rigidbody starts behaving abnormally. Sometimes it wont collide with anything, sometimes it would just keep jumping without any reason, sometimes applied force sets it to wrong direction. Which happens randomly for different combinations of transforms.
But if I just change some rigidbody parameter from inspector and revert it (e.g. if i check and uncheck useGravity), it starts to behave normally again.
Is there something I need to do like refreshing the ridigbody or something?
When you re-enable a script its Start() will not run. You have to invoke it manually.
@Landern it was called from Update().... now I tried FixedUpdate()..... but no improvemnt
@meat5000 The issue is not that the scripts are not being enabled..... however I have OnEnable() & OnDisable() defined in those.
@Landern Just now I tried putting this code in Release().... and everything works fine
player.active = false;
player.active = true;
But dont think this is a good solution. :P
You have this?
function OnEnable()
{
Start();
}
The issue I stated is not that the scripts are not being enabled, but any rigidbody setup code in the Start methods simply will not run as Start will only run once in the entire lifetime of the object.
Your answer
Follow this Question
Related Questions
Ignore Collision Temporarily 1 Answer
How to properly move a rigidbody/collider? 2 Answers
Modifying height directly on a Rigidbody Object without gravity 1 Answer
Is there a solution to when colliders bypass? 2 Answers
Punching a crate 0 Answers