Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

newbie question about rigidbodies -> indiana jones style…

Discussion in 'Scripting' started by konzeptzwei, Feb 10, 2006.

  1. konzeptzwei

    konzeptzwei

    Joined:
    Feb 10, 2006
    Posts:
    63
    hi folks!

    i have the following setup in my scene: a character, start point, a rotating bridge and an end point.

    now i would like to jump via the Firt Person Controller Prefab on the rotating bridge and »travel« to the end point. my problem is that the FPC doesn't »stick« to the bridge. the bridge does rotate under my feet but i just stand there.

    i tried first to throw a cube with a rigidbody a the bridge to see if it somehow sticks to it but unfortunately it doesn't…

    …what am i doing wrong?

    cheers.chris
     
  2. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    if you are rotating your bridge with transform.rotate, it will rotate the bridge independently of the physics engine. Every frame the physics engine never gets told that the bridge even rotated so it never applies friction on your FPS controller.

    how you can do stuff like this is use a hinge joint. Put a hinge joint component on your bridge, set it up so that the rotation axis is up/down and the rotation point is in the middle of the bridge. Now it needs a way for the bridge to rotate when the player steps onto it. Uncheck the boxes Use Motor and Use Limits on the hinge joint component in the inspector and under the Spring category set Spring to enough force to rotate the bridge and Damper to some number lower then Spring like 1 or 3. Leave target position at 0. Now you are almost ready. Go into your script and edit it to do something like this:

    Code (csharp):
    1.  
    2.  
    3.  
    4. var bridgeTurned = false;
    5.  
    6. function OnCollisionEnter()   // Presumably when the bridge collides with the player. if the bridge hits the floor or a wall this will get called.
    7. {
    8.  
    9.     if(bridgeTurned == false)
    10.     {
    11.         // the bridge will have the force of hingejoint.spring.spring applied with hingejoint.spring.damper drag applied to rotate it twords this rotation
    12.         hingeJoint.spring.targetPosition = 180;
    13.     }
    14.     else
    15.     {
    16.         // the bridge will have the force of hingejoint.spring.spring applied with hingejoint.spring.damper drag applied to rotate it twords this rotation
    17.         hingeJoint.spring.targetPosition = 0;
    18.     }
    19. }
    20.  
    21. function OnCollisionExit()   // Presumably when the  player leaves the bridge.
    22. {
    23.  
    24.     if(bridgeTurned == false)
    25.     {
    26.         // next time the player steps on the bridge it will swing back
    27.         bridgeTurned = true;
    28.     }
    29.     else
    30.     {
    31.         // next time the player steps on the bridge it will swing back again
    32.         bridgeTurned = false;
    33.     }
    34. }
    35.  
    36.  

    This isn't perfect but you get the idea. edit the hinge joint settings to your liking
     
  3. konzeptzwei

    konzeptzwei

    Joined:
    Feb 10, 2006
    Posts:
    63
    hi

    thanks for the almost immediate reply! well i checked out your code and… unfortunately it doesn't work like it should…

    i once again used a cube to let it fall down on the bridge but it's not very reliable…sometimes the bridge mpves -> sometimes not. then i tried to let the first person controller fall down on the bridge an it didn't work at all…the bridge seems not to recognize the first person controller as a trigger.

    another problem is that the object has to fall down in order to rotate the bridge…the bridge should act more like a carussel…

    like this: http://www.chris-site.de/preview.mp4

    well seems to be i am just a bit too stupid for unity…

    cheers.chris
     
  4. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    Your not too stupid to use Unity, I'm to stupid to understand that you want the bridge to be constantly rotating.

    Thats so easy you don't even need a script.

    Attach a hinge joint to your bridge, uncheck the limits and spring boxes and check the motor box. Then type in the motor catagory the degrees per second you want the bridge to rotate at for Target Velocity, set Force to a high number and make sure Free Spin is unchecked.

    Thats all!
     
  5. konzeptzwei

    konzeptzwei

    Joined:
    Feb 10, 2006
    Posts:
    63
    well i feel that i am getting on your nerves BUT i still face two problems:

    1. the first person controller doesn't work with this setup -> the cube works however. i think it has to do with the charcter controller component but i don't know how to tell fpc to behave like the cube…

    2. the rotation is a problem too. i did setup the hinge joint like you told me and it works -> unfortunately it is too fast, so the cube falls down. so i tried to enter different values to let it rotate slower. if the values are good for a slower setup the bridge doesn't rotate automatically. it only rotates when the cube falls down on it…well it's not that severe but point 1. is…

    thanx again for your help!
    cheers.chris
     
  6. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    Your not getting on my nerves, I hope you get this working and learn more and more about unity so I'm happy to help.
    Sometimes the physics can be a bit confusing but there is always a reason why that you can find out and solve. I'm not sure what that reason is so I'm going to create a very simple project with what you want to achive. You can then compare the two and see what your problem was, then learn from it.

    <returns from test project creation>

    The FPS walker scripts incuded with unity don't use physics to determine thier location ect. So if something moves under them they never get any reason to move with it. I have created an FPS walker script that depends on physics and friction and built a test scene with it. it is a little sloppy moving but it works for what you are trying to do.

    Here is the project file:
    www.yoggy.bluegillweb.com/Files/Project.zip

    Here is a built game version:
    www.yoggy.bluegillweb.com/Files/Player.zip
     
  7. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    This is great stuff Yoggy, you've got some skills being able to whip that up so fast! I'm going to learn quite a bit from this example towards scripting a better player controller for myself. Thanks a lot.
     
  8. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
  9. konzeptzwei

    konzeptzwei

    Joined:
    Feb 10, 2006
    Posts:
    63
    hey yoggy! AWESOME! i just tried the player and it does EXACTLY what i want! tomorrow i'll check out the project file…

    …thanx man, i start to fall in love with unity AND the community :D

    cheers from cologne, germany!
    chris
     
  10. konzeptzwei

    konzeptzwei

    Joined:
    Feb 10, 2006
    Posts:
    63
    hey there!

    i just tried to check your new project file that you loaded up yesterday and there seems to be a bug in the new project file -> the camera doesn't »stick«. i tried the player and there seems to be the same problem…the old (first) player works fine.

    could you upload the old »bugfree« version again?

    thnx.chris :roll:
     
  11. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    Ok I updated it again. now it should work. Sorry about that.