Search Unity

Grapple and swing/pull mechanic

Discussion in 'Scripting' started by 8r3nd4n, Feb 3, 2012.

  1. 8r3nd4n

    8r3nd4n

    Joined:
    Sep 10, 2011
    Posts:
    30
    Hi all

    I need to implement a grapple/hook mechanic into a game I am making and have looked around various forums as to how to do so but am still a bit stuck when it comes to the actual coding.
    I have borrowed elements from here: http://forum.unity3d.com/threads/36503-Grappling-Hook but am not sure how to get it working how I want.
    So far I have an object called shooter attached to the hand of the player model.
    When the user pushes 'g', the shooter shoots out and gravity is applied so that it behaves realistically. after 2 seconds, if it hasnt hooked onto anything, it returns to the characters hand position and gravity is removed so it follows properly. This part I have almost working as I want (except for some vector 3.lerp problems which I would also appreciate help with).
    What I want to do is if the grapple hits a grapple point, it stays and the character swings like a pendulum until they push a button to jump off. this could be achieved through spring joints somehow but not sure how to implement properly.
    If the character was to push up, they would move up the rope to the grapple point, which would be positional transforms.
    The game is 2D based so only need to move in the x,y directions.
    So far this is what I have attached to the shooter object:

    Code (csharp):
    1. var shootForceX=0;
    2. var shootForceY=0;
    3. var shootForceZ=0;
    4. var smooth = 5.0;
    5. private var hooked : boolean = false;
    6.  
    7. rigidbody.useGravity = false;
    8.  
    9. function Update ()
    10.  
    11. {
    12.  
    13.  if(Input.GetKeyDown("g"))
    14. {
    15. rigidbody.useGravity = true;
    16. //...shoot the spawned grappling hook with the forces set in the variables
    17. rigidbody.AddForce(shootForceX,shootForceY,shootForceZ);
    18.  
    19.     if (hooked == false)
    20.     {
    21.     returnShooter();
    22.     }
    23.  
    24. }
    25. }
    26.  
    27.  
    28. function returnShooter() //returns the shooter to the hand position
    29. {
    30.     yield WaitForSeconds (2);
    31.     rigidbody.useGravity = false;
    32.         rigidbody.velocity = Vector3(0,0,0);
    33.     rigidbody.AddForce(0,0,0);
    34.     transform.localPosition = Vector3(0.0f, 0.0f, 0.0f);
    35.     //transform.position = Vector3.Lerp(transform.position, transform.localPosition , Time.deltaTime * smooth);
    36. }
    37.  
    38.  
    Any help greatly appreciated
     
  2. 8r3nd4n

    8r3nd4n

    Joined:
    Sep 10, 2011
    Posts:
    30
    I have added some more and have some functionality working but it still does not behave how I need it to.

    Once again, this script is attached to an object (grappling hook) that is parented to a character models (puppet) hand.
    it fire from the hand alright but still cant get the movement of the character right after that.
    I think the best thing would be to have a hinge/joint that when the player hits a grapple point, the hinge is enabled and they swing like a pendulum under the grapple point until a button is pressed and the hinge is disabled. When I have tried this, I cant seem to get the hinge to disconnect from the player and to attach it to a different grapple point.
    Because I am using a character controller, the physics are a real pain since adding the rigid body and gravity to the puppet causes it to go crazy.
    Another idea I had was that the puppet could ping pong between 2 vector3's when on a grapple point until a button is pressed and they jump off, but am unsure also if this can be implemented.

    Code (csharp):
    1. var shootForceX=0;
    2. var shootForceY=0;
    3. var shootForceZ=0;
    4. var smooth = 5.0;
    5.  
    6. var isShooting = false;
    7. var isHooked = false;
    8. var timeShot = 0;
    9. var grappled = true;
    10.  
    11. private var GrapplingHitPoint;
    12. public var swingPoint : Vector3;
    13.  
    14.  
    15. function Update ()
    16. {
    17.     if(Input.GetKeyDown("g")  !isShooting)
    18.  
    19.     {
    20.             rigidbody.useGravity = true;
    21.             rigidbody.AddForce(shootForceX,shootForceY,shootForceZ);
    22.  
    23.             isShooting = true;
    24.             timeShot = Time.time;
    25.     }
    26.  
    27.     if(isShooting)
    28.  
    29.         {
    30.             if(Time.time > timeShot + 2)
    31.             {
    32.             isShooting = false;
    33.             grappled = false;
    34.             retracting = true;
    35.             }
    36.  
    37.             if(!isHooked)
    38.             {
    39.             returnShooter();
    40.             }
    41.  
    42.             if(grappled)
    43.             {
    44.             IsSwinging();
    45.             }
    46.  
    47.             if(retracting)
    48.             {
    49.             returnShooter();
    50.             }
    51.         }
    52. }
    53.  
    54.  
    55. function IsSwinging()
    56.     {
    57.     bob = GameObject.Find("Puppet");
    58.     //move the puppet to the grapple point
    59.     //maybe mathf.pingPong can make the character keep moving back and forth
    60.     bob.transform.position = Vector3.Lerp(bob.transform.position, swingPoint+ Vector3(6,-1,0), Time.deltaTime * smooth);
    61.  
    62.     }
    63.  
    64. function returnShooter() //returns the grappling hook to the hand position
    65.     {
    66.         yield WaitForSeconds (0.5);
    67.         rigidbody.useGravity = false;
    68.         rigidbody.isKinematic = false;
    69.         rigidbody.velocity = Vector3(0,0,0);
    70.         rigidbody.AddForce(0,0,0);
    71.         transform.localPosition = Vector3(0.0f, 0.0f, 0.0f);
    72.     }
    73.  
    74. function OnCollisionEnter(collision : Collision)
    75.     {
    76.         if(collision.gameObject.CompareTag("GrapplePoint"))
    77.         {
    78.         print("HitGrapple");
    79.         isShooting = false;
    80.         colliding = true;
    81.         rigidbody.isKinematic = true;
    82.         grappled = true;
    83.  
    84.         swingPoint.x = collision.transform.position.x;
    85.         swingPoint.y = collision.transform.position.y;
    86.         swingPoint.z = collision.transform.position.z;
    87.  
    88.         //This gets the character object...
    89.         bob = GameObject.Find("Puppet");
    90.  
    91.         //wait before returning the grappling hook to hand position
    92.         yield WaitForSeconds (1.5);
    93.         returnShooter();
    94.         }
    95.     }