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.
  2. Dismiss Notice

Creating Grab/Throw Mechanic in 2D Platformer

Discussion in 'Scripting' started by Rxanadu, Jul 12, 2014.

  1. Rxanadu

    Rxanadu

    Joined:
    May 24, 2012
    Posts:
    50
    I've been making a grab/throw system reminiscent of Mischief Makers. Here's what I've come up with:
    When Player grabs Enemy:
    • Enemy's collider is set to a trigger, preventing it from harming Player
    • Enemy's position is set to the same position as Player's attack hitbox
    • Enemy's move rate, velocity are set to 0, preventing it from moving around
    • Enemy has a timer set for breaking out of Player's grab
      • if Enemy escapes Player's grab, Player is pushed backwards, and Enemy resumes Attack/Patrol states
    When Player throws Enemy:
    • Enemy's collider is set back to basic collider
    • Enemy's move rate, velocity are set to normal state
    • Enemy is sent flying via throwing force
    • Player is pushed backward from direction of throwing direction

    When Enemy is thrown:
    • Enemy is damaged after colliding with other objects defined as "Ground"
    • Enemy is reset to Patrolling/Attack states after a short delay

    Main issues with grab/throw system at the moment:
    • can't find a good way to switch enemy from Thrown state back to Patrolling/Attacking state
    • unable to get a good throwing arch for enemies (enemies tend to slide across the ground when thrown)
    • unable to stop enemy's previous velocity from pushing the player around when grabbed
      • This may be happening when I parent the enemy to the player's attack hitbox transform, but not sure
    If anyone has tips on properly implementing a grab/throw mechanic similar to the one found in Mischief Makers, I would much appreciate it. Thank you for taking the time to read this.
     
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    You probably shouldn't use the IsTrigger, use a boolean in your script for that.
    Doesn't it create an arc if you just add force to the enemy?

    Code (CSharp):
    1. Enemy.rigidbody.AddForce(direction, ForceMode.Impulse);
    When do you want to switch the enemy's state from patrolling to attacking?
     
  3. Rxanadu

    Rxanadu

    Joined:
    May 24, 2012
    Posts:
    50
    Here's the code I'm currently using for grabbing and throwing enemies:
    Code (CSharp):
    1. void GrabEnemy(Transform enemy){
    2.  
    3.         //make sure the enemy knows it's been grabbed
    4.         enemy.GetComponent<Enemy>().IsGrabbed=true;
    5.        
    6.         //prevent enemy from falling out of player's hands
    7.         enemy.position = transform.position;
    8.         enemy.parent = transform;
    9.         enemy.rigidbody2D.gravityScale = 0.0f;
    10.     }
    11.  
    12.     void ThrowEnemy(Transform enemy){
    13.         float h = Input.GetAxis ("Horizontal");
    14.         float v = Input.GetAxis ("Vertical");
    15.  
    16.         //drop enemy
    17.         enemy.parent = null;
    18.  
    19.         //stop grabbing the enemy...
    20.         enemy.GetComponent<Enemy>().IsGrabbed = false;
    21.        
    22.         //...and throw the enemy
    23.         enemy.GetComponent<Enemy>().IsThrown=true;
    24.  
    25.         //push them away from player
    26.         //Vector2 throwVector = (transform.right * Mathf.Sign (transform.parent.transform.localScale.x)) * throwForce;
    27.         Vector2 throwVector = new Vector2(h,v);
    28.         enemy.rigidbody2D.AddForce (throwVector * throwForce);
    29.         enemy.rigidbody2D.gravityScale = 1.0f;
    30.  
    31.         //push player back, as well
    32.         transform.parent.rigidbody2D.AddForce (-throwVector * throwForce);
    33.     }
    As for the switching between Patrolling and Attacking: that's handled by the enemy itself when it sees the player.
     
  4. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    You said
    Can you be more specific on that question?
     
  5. Lahlou_ex

    Lahlou_ex

    Joined:
    Mar 24, 2017
    Posts:
    1
    Please tell me you have found a way to implement it correctly. I am in desperate need of exactly this mechanic for my game!!
     
  6. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Ask the question as your own thread