I’ve got a character that is a rigid body flying through space. I want to set it up so that the character is “self leveling”, so if it collides with another object and rolls off level, it will automatically return to a level state.
I experimented with the object’s “center of gravity”, but that only effects it’s resistance to rolling and didn’t return it to level. So now I’m trying to do this with relativeTorque.
Here’s my code so far:
var rollRight = 0.1;
var rollLeft = -0.1;
function FixedUpdate () {
if (transform.localEulerAngles.z < 0){
rigidbody.AddRelativeTorque (0,0,rollRight);
}
if (transform.localEulerAngles.z > 0){
rigidbody.AddRelativeTorque (0,0,rollLeft);
}
}
I experimented with different values for “rollRight” and “rollLeft”, but whatever I try it puts the object into a permanent spin.
Interestingn fix. It didn’t work so well for me (maybe because my character mass is really low?) but I see where you are going. Pretty cool 'out of the box" thinking.
Did you remember to add this at the top of the last script snippet?
var rollRight = 0.1;
var rollLeft = -0.1;
You can’t call a variable without first declaring it and giving it a value. Play with the values until you get it the way you like it. The only possible glitch I haven’t fixed is if the vehicle is turned -exactly- 180. There is a chance it will stay there. Don’t know, I’ll have to play with it to see if it’s an issue. If it is, I’ll just change the 2 “180” values.
…hmm… since this is my first Unity project, not sure how it’s normally done. I set the center of mass at the object’s center and gave it a mass of 1. I left the gravity at -9.8 and gave the object an upward force of +9.8. Seems to work perfectly for me, although I may move the COG down a tiny bit to make it more stable. But I’m trying to simulate a submersible and you’re trying to simulate a space craft, so I guess there will be some differences.
Have you looked at Neil’s “Pheonix” code yet? I played with it a bit this morning and I really like how he’s got his flight controls set up. Very fluid and natural feeling.
I’m actually trying to set up an insect. Low mass, high relative drag (although I don’t know how to do air drag). I will plug in the var calls first. You were right, I didn’t include that.
You might want to do some homework on the physics involved and then ultimately write a “flight model” for your insect. It sounds more difficult than it really is, but you’ll need to get some basic info together first. There are a couple of good books in print on simulating physics in games, I’ll see if I can find a link or two for you…
I can’t help thinking, if you want darting around like a fly, then Descent or Descent 3 could be a good inspiration for the flight system. (Especially with the optional auto-level On.)
Good call Morgan. I remember that series.
The game I am using as a model right now is called Crimson Skies for XBox.
Simple mechanics, multiple planes with different abilities but the same key combinations, self righting…simple. The mechanics are unrealistic to some extent, but that’s not the point of the game.
If I were actually going to recreate a Fly’s flight accurately, it would be completely unplayable. they move way too fast and aggresively to have any meaningful control. I suppose balance will be the most difficult thing.