Search Unity

Help with rigidbody script?

Discussion in 'Editor & General Support' started by bigkahuna, Aug 30, 2006.

  1. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    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:

    Code (csharp):
    1. var rollRight = 0.1;
    2. var rollLeft = -0.1;
    3.  
    4. function FixedUpdate () {
    5.     if (transform.localEulerAngles.z < 0){
    6.         rigidbody.AddRelativeTorque (0,0,rollRight);
    7.     }
    8.     if (transform.localEulerAngles.z > 0){
    9.         rigidbody.AddRelativeTorque (0,0,rollLeft);
    10.     }
    11. }
    I experimented with different values for "rollRight" and "rollLeft", but whatever I try it puts the object into a permanent spin.

    Any ideas? Thanks!
     
  2. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Ah! Figured it out. "transform.localEulerAngles" is a number between 0 and 359, I was assuming it could have a negative value. Oops. Fixed it.
     
  3. Macupuncture

    Macupuncture

    Joined:
    Jun 13, 2005
    Posts:
    179
    So in this case, the '.z<0" becomes ".z>0"?

    Does that fix it? Please elaborate if you will. Thanks.
     
  4. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    '.z<0' becomes '.z>180' and '.z>0' becomes '.z<180'
     
  5. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Here's the corrected code:
    Code (csharp):
    1.     if (transform.localEulerAngles.z > 180  transform.localEulerAngles.z < 358){
    2.         rigidbody.AddRelativeTorque (0,0,rollRight);
    3.     }
    4.     if (transform.localEulerAngles.z > 2  transform.localEulerAngles.z < 180){
    5.         rigidbody.AddRelativeTorque (0,0,rollLeft);
    Notice that I allowed the object to be off level by as much as 2 degrees, otherwise it continually tries to correct itself.
     
  6. Macupuncture

    Macupuncture

    Joined:
    Jun 13, 2005
    Posts:
    179
    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.
     
  7. Macupuncture

    Macupuncture

    Joined:
    Jun 13, 2005
    Posts:
    179
    Hmm. Now I'm getting errors the rollRight and rollLeft are not valid.

    Hmph. Stupd machines...
     
  8. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Did you remember to add this at the top of the last script snippet?
    Code (csharp):
    1. var rollRight = 0.1;
    2. 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.
     
  9. Macupuncture

    Macupuncture

    Joined:
    Jun 13, 2005
    Posts:
    179
    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.
     
  10. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    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...
     
  11. Macupuncture

    Macupuncture

    Joined:
    Jun 13, 2005
    Posts:
    179
    Much obliged. I'll do some research on my own and we can pool resources.
     
  12. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    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.)
     
  13. Macupuncture

    Macupuncture

    Joined:
    Jun 13, 2005
    Posts:
    179
    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.