- Home /
Camera Rigidbody
Hello,
Okay, so in my game when you press esc it comes up with the options, i added a rigidbody and box collider to the camera, so when you press esc the camera drops to the floor which looks really cool!, but there is a problem when i'm playing the game i can shoot straight and the character goes weird because of the camera rigidbody etc, how can i fix it, so when you press esc the camera has a rigidbody and collider and drops to the floor like what i said before,
Thanks for the help!
Answer by Kleptomaniac · Apr 21, 2012 at 07:00 AM
Have you tried enabling your Box Collider and Rigidbody when escape is first pressed and then disabling when pressed again? Since enabling and disabling these two components is not possible there are two possible workarounds you could try. The first involves disabling your collider and setting your rigidbody to kinematic (therefore making it not driven by the physics engine). While this would be the most efficient way to do it, I'm not sure if it will actually work. If it does, I would do it like this:
private var esc : boolean = false;
private var rigid : Rigidbody;
private var coll : BoxCollider;
function Start () {
rigid = GetComponent(Rigidbody);
coll = GetComponent(BoxCollider);
rigid.isKinematic = true;
coll.enabled = false;
}
function Update () {
if (Input.GetKeyDown(KeyCode.Escape)) {
if (!esc) {
rigid.isKinematic = false;
coll.enabled = true;
esc = true;
} else {
rigid.isKinematic = true;
coll.enabled = false;
}
}
}
If that doesn't work however, I would try destroying the rigidbody component and then using AddComponent every time you enter your escaped state. This won't be very efficient, however it's the only alternative I can think of.
Hope that helps, Klep
Hello,
Thanks for the help,
It works but the character seems twitchy, and won't shoot straight with this, i've tried other ways to, it worked the same as this but the character goes weird.
EDIT: i think it's to do with the gravity.
Thanks
Well, here's another idea. Why not have two cameras? One which is just your normal main camera, the other is your escape camera, which has your rigidbody and box collider attached to it. When you toggle between your game states, disable your main camera and enable your other camera and vice versa.
However, thinking about it, I'm not sure how rigidbodies are affected by disabled objects. Give it a go for me. :)
Hello,
Couldn't really get it to work, I Tried your script for the first post again!, Worked like a charm! :), but it only happens the first time you press esc, so when i press esc it drops, when i press it again it goes back to normal, but if i press esc again its still normal.
Thanks! :)
Oh, yeah, sorry! I forgot to negate the esc boolean in the else statement. Replace:
} else {
rigid.is$$anonymous$$inematic = true;
coll.enabled = false;
}
with:
} else {
rigid.is$$anonymous$$inematic = true;
coll.enabled = false;
esc = false;
}
That should work. :)