Search Unity

Rolling up walls for a Ball, scripts?

Discussion in 'Scripting' started by Chaotics, Apr 27, 2008.

  1. Chaotics

    Chaotics

    Joined:
    Apr 27, 2008
    Posts:
    3
    Hi all,

    I am developing a game that has a fairly simple premise, you are a ball and have to roll through a rollar coaster type setting and hit 'enemies' that are along the way. I am new to Unitys scripting, and was wondering if anyone can point me in the right direction in what I could do to get the ball rolling up walls and to turn upside down. Examples of the level can be seen in the Environment Progress article here http://phroztee385.blogspot.com/. Any help with what scripts to use would be greatly greatly appreciated, thankyou all :)
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Here's a start:


    Code (csharp):
    1. var rollForce = 2.0;
    2. var maxSpin = 10.0;
    3. var stickeyness = 5.0;
    4. private var grounded = false;
    5. private var stickPos : Vector3;
    6.  
    7. function Start ()
    8. {
    9.     rigidbody.maxAngularVelocity = maxSpin;
    10. }
    11.  
    12. function FixedUpdate ()
    13. {
    14.     var verInput = Input.GetAxis ("Vertical");
    15.     var horInput = Input.GetAxis ("Horizontal");
    16.    
    17.     rigidbody.AddTorque (Vector3 (verInput, 0.0, -horInput) * rollForce);
    18.    
    19.     // Stick to walls
    20.     if (grounded)
    21.     {
    22.         var direction = (stickPos - transform.position).normalized;
    23.         rigidbody.AddForce (direction * stickeyness);
    24.     }
    25. }
    26.  
    27. function OnCollisionStay (collision : Collision)
    28. {
    29.     grounded = true;
    30.    
    31.     var contact = collision.contacts [0];
    32.     stickPos = contact.point;
    33. }
    34.  
    35. function OnCollisionExit ()
    36. {
    37.     grounded = false;
    38. }
    39.  
    40. @script RequireComponent (typeof Rigidbody)
     
  3. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Hey!
    Look at my game, it's almost the same (except my game has lousy art).
    http://arongranberg.com/unity/marbles.html (wasd, arrows and mouse)
    The game is only in testing mode so the marble got the wrong texture and much of the things is wrong, but overall it works fine.

    PS: Your art was really nice :D

    PPS: and "F" to go into first person mode (R to zoom)