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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Trying to attach my 2D character to a hook

Discussion in '2D' started by TheIrishKraken, Aug 19, 2019.

  1. TheIrishKraken

    TheIrishKraken

    Joined:
    Nov 26, 2013
    Posts:
    4
    Hello

    I am trying to implement a mechanic in my game where my 2D character can attach himself to hooks by just jumping onto them. I cant figure out how to best do this.
    I have all the code in place where my hooks collider can detect my players collider when he passes through it.
    What I want is that when I jump onto the hook I want the player to stick to it and from there only allow him to jump, so basically freeze him in that position until I hit the space bar.
    I have tried freezing the players ridgidbody constraints when he passes through the hooks collider but this has has a weird result where the player gets stuck on the x axis and I can move him left and right.
    The script attached to the hook should do:

    If (hook collider detects the player) {
    freeze the player in position/ disable gravity for player
    disable the players movement
    if space bar is pressed then Jump()
    resume all player movement
    }
     
  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Have you tried setting the player's velocity to zero upon trigger impact?
    Code (CSharp):
    1. private Rigidbody2D playRb;
    2.  
    3. void Start()
    4. {
    5. playerRb = GetComponent<Rigidbody2D>();
    6. }
    7.  
    8. void OnTriggerEnter2D(Collider2D collision)
    9. {
    10. if(collision.tag == "Player")
    11. {
    12. playerRb.velocity = Vector2.zero;
    13. }
    14. }
    So you'd basically need a bool variable "playerIsHooked" set to false from start. Set it to true upon collision trigger, and set it back to false when the space bar is fired. Then you might also need to set the player's velocity to ONLY be zero if the playerIsHooked is true. Otherwise, he will be stuck.
     
  3. TheIrishKraken

    TheIrishKraken

    Joined:
    Nov 26, 2013
    Posts:
    4
    I implemented what you suggested but the player does not stay attached.
    When I jump into the trigger I can see the velocity lowering on the player but he still falls down.
    Capture2.JPG

    Here is the full script attach to the hook game object:

    Capture3.JPG
    Any other ideas would be greatly appreciated. I have tried looking online but cant seem to find an answer. I know I can freeze his 2D collider when he enters the trigger but It had strange results like I mentioned before, unless I was implementing it wrong.
     

    Attached Files:

  4. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Oh, my bad, I forgot about that.

    Code (CSharp):
    1. playerRigidBody.useGravity = false;
    And you simply turn it back on when you jump.

    Let's say you have your jump script on another script like you should. Do this:

    Code (CSharp):
    1.  
    2. public static bool gravityIsOn;
    3.  
    4. private void Start()
    5. {
    6. gravityIsOn = true;
    7. // if you have scenes that may NOT have gravity on from the start,
    8. // you will need to make this false in that particular scene Start()
    9. // Which may lead to a conflict. Just keep this in mind
    10. }
    11.  
    12. // Not typing your on trigger function code out, just imagine all your code is here plus this
    13. gravityIsOn = false;
    14. playerRigidBody.useGravity = gravityIsOn;
    15. // Now you can change this variable in your jump script easily
    16. // Let's say you jump in a script called PlayerMovement
    17.  
    18. // *** Magically we're now in your PlayerMovement script ***
    19. // I'm also making an example jump
    20.  
    21. if(Input.GeyKeyDown(KeyCode.Space)
    22. {
    23. AttachToHook.gravityIsOn = true;
    24. playerRigidBody.useGravity = AttachToHook.gravityIsOn;
    25. // ----- Your jump script would be here -----
    26. }
    27.  
     
    Last edited: Aug 22, 2019
  5. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Ok, that code I wrote looks like crap so I'm going to clean it up for ya.

    Code (CSharp):
    1. // ********* AttachToHook Script *********
    2.  
    3. public static bool gravityIsOn;
    4. private void Start()
    5. {
    6. gravityIsOn = true;
    7.  
    8. }
    9. // Not typing your on trigger function code out, just imagine all your code is here plus this
    10. gravityIsOn = false;
    11. playerRigidBody.useGravity = gravityIsOn;

    Now the Player Movement script

    Code (CSharp):
    1. // ******** In your Jump Script or where ever you do this action *********
    2.  
    3. if(Input.GeyKeyDown(KeyCode.Space)
    4. {
    5. AttachToHook.gravityIsOn = true;
    6. playerRigidBody.useGravity = AttachToHook.gravityIsOn;
    7. // ----- Your jump script would be here -----
    8. }
     
  6. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    I just noticed you have player with your player movement script component attached.
    You have the script already attached to your player, correct?

    Code (CSharp):
    1. private Rigidbody2D playerRigidBody;
    2. private PlayerMovement player;
    3.  
    4. private void Start()
    5. {
    6. playerRigidBody = GetComponent<Rigidbody2D>();
    7. player = GetComponent<PlayerMovement>(); // This only contains the script component called PlayerMovement you can set enabled=true/false and that's about it.
    8.  
    9. // This is much faster than using Find()
    10. // You can use this as long as the AttachToHook script is on your player's GameObject.
    11. // You only need to use Find() if the script is on the hook and you need to find your player.
    12. // If the script is on your player, it's already found ;)
    13. }
     
  7. TheIrishKraken

    TheIrishKraken

    Joined:
    Nov 26, 2013
    Posts:
    4
    Yes I have a script called PlayerMovement attached to the Player gameobject.
    The AttachToHook script is attached to the Hook game object.

    Ok I will try out the code you provided for switching gravity on/off tonight and let you know the results.
     
    MisterSkitz likes this.
  8. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    I'll try to be on standby if you have any questions. I am going to sleep pretty early tonight, however, I will check back in tomorrow if I'm asleep before you get back here. Good luck, amigo! :)