Unity 2D: Grab Objects

Поділитися
Вставка
  • Опубліковано 29 сер 2024
  • Learn how to grab and release objects with a player. Super easy and beginner friendly tutorial.
    ✅ Get my courses with discount:
    👉 Unity 2D Master: www.udemy.com/...
    👉 Unity Mobile Course: www.udemy.com/...
    👉 Unity Input System with Rebinding: www.udemy.com/...
    👍 Save your time and learn Unity faster
    -------------------------------------------------------
    🔔 Subscribe for more awesome content:
    🔴 / @rootgames
    -------------------------------------------------------
    🟡 Support this channel through donations
    Any amount means a lot and will help grow the channel:
    👉 PayPal: www.paypal.com...
    -------------------------------------------------------
    Social Networks:
    ⭐ Instagram: / rootgamesofficial
    ⭐ Twitter: / rootgamescro
    ⭐ TikTok: www.tiktok.com...
    ⭐ Facebook: / root-games-10337038860...
    -------------------------------------------------------
    #unity2d #unitytutorial #unity2dplatformer #rootgames

КОМЕНТАРІ • 55

  • @Foxtrot6624
    @Foxtrot6624 Рік тому +8

    I finished the game I was trying to make for my college thesis, and the amount of time I spent smashing my head against a wall trying to figure out how to do this was incalculable. Fantastic and elegant solution, I ended up just using hinges and it was not good. Am revisiting the game as a personal project and I wish this tutorial was around back then. Great stuff.

    • @RootGames
      @RootGames  Рік тому +1

      Thank you, and good luck with the project!

  • @TX20
    @TX20 2 роки тому +9

    Thanks for sharing!
    This is probably as beginner-friendly as it gets

  • @RootGames
    @RootGames  2 роки тому +11

    IMPORTANT: The direction of shooting the ray depends on how you flip the player. If you use rotation for flipping the player, then the code will work (you can check my shooting tutorial in which I flip the player using rotation- transform.Rotate(0,180,0))
    If you use the scale (-1), then instead of transform.right you need to put your variable that keeps the information of the player's direction. For example, direction=1 when the player is facing right, direction=-1 when facing left.

  • @ilizzium1241
    @ilizzium1241 2 роки тому +2

    if you take an object and push another with it, it will fall through the collider. if you disable interaction with layers. then upon contact, the object begins to receive infinite velocity. how to solve*?

  • @simonbahamonde7370
    @simonbahamonde7370 2 роки тому +2

    Hey! I did something similar but I'm having a problem when moving further away from the original position of the object. If the camera stop seeing the original position of it, the sprite is not visible anymore.

  • @LionelvsJapanTV
    @LionelvsJapanTV 2 роки тому +1

    Also, when I pick up an the box and look another direction, the box doesnt flip with my character. it stays on one side

  • @clever_xd
    @clever_xd Рік тому

    if you want your object to rotate and drop with correct position, change string to "RaycastHit2D hitInfo = Physics2D.Raycast(rayPoint.position, grabPoint.localPosition, rayDistance);"
    and add in void update next:
    if (grabbedObject != null)
    {
    grabbedObject.transform.position = grabPoint.position;
    }

  • @fabianbosl4483
    @fabianbosl4483 6 місяців тому

    Thanks for the Great Input! Somehow I had troubles releasing the Box again. I solved it as followed. I used Unitys "using UnityEngine.InputSystem;" Make sure you implement the InputSystem into your Player as an Component
    //GRABBING
    public void Grab(InputAction.CallbackContext context)
    {
    RaycastHit2D hitInfo = Physics2D.Raycast(rayPoint.position, transform.right, rayDist);
    if (hitInfo.collider != null && hitInfo.collider.gameObject.layer == layerIndex)
    {
    if (context.performed && grabbedObject == null)
    {
    grabbedObject = hitInfo.collider.gameObject;
    grabbedObject.GetComponent().isKinematic = true;
    grabbedObject.transform.position = grabPoint.position;
    grabbedObject.transform.SetParent(transform);
    }
    else if (context.performed && grabbedObject != null) //Checks if there is already a Box grabbed
    {
    grabbedObject.GetComponent().isKinematic = false;
    grabbedObject.transform.SetParent(null);
    grabbedObject = null;
    }
    }
    }

  • @fabiovie92
    @fabiovie92 2 роки тому +1

    Thx dude, I'll need It soon

  • @gegesteorball
    @gegesteorball Рік тому +1

    Finnaly Its Working ! Thanks !

  • @nicolaspierson8575
    @nicolaspierson8575 Рік тому +1

    Does it need any components in GrabPosition and RayPosition to work?

  • @guldestehazrlkb8412
    @guldestehazrlkb8412 9 місяців тому +1

    ty for video, its working but i want to drag just 1 object,how can i do that?

  • @jonathanpaz9250
    @jonathanpaz9250 3 місяці тому

    the box is kinematic so it can go through other colliders, how do you fix that

  • @beshkekart3909
    @beshkekart3909 2 роки тому +1

    Thank you,root!
    Guys, if u have some problems with input system write me

  • @hrishikeshgarud7177
    @hrishikeshgarud7177 2 роки тому +2

    Thanks

  • @aymange4377
    @aymange4377 2 місяці тому

    dosent work for me whene i press space player grabed object but whene i pres again space dont want to release

  • @Phloxx1
    @Phloxx1 10 місяців тому +1

    are rayposition and grab position objects empy Objects?

  • @youratrucker20
    @youratrucker20 Рік тому +1

    I have a problem where it says that the keyboard does not appear within the context how do i fix this???

    • @RootGames
      @RootGames  Рік тому

      As I mention in the video, I use the new input system, so you need to import it with the package manager. But you can do the same thing using the old system.

  • @MagnusRazer
    @MagnusRazer Рік тому

    Podrias utilizar un evento asociado a un Trigger "Contenedor" de espacio. Asi desligar de tu Player , una variable con el objeto. ¿no?. Excelente contenido!

  • @xandeco420
    @xandeco420 4 місяці тому

    CARAHLO EU NAO TINHA PENASDO EM FAZER ISSO

  • @icdayroicom2706
    @icdayroicom2706 2 роки тому +1

    VERY USEFUL

  • @trollofthedungeon9196
    @trollofthedungeon9196 2 роки тому +1

    Thanks for the tutorial! However, I do have a small problem. For some reason, although no errors are being thrown, my character is still not picking up the box. Also, how do I set the key to another key? my space bar is currently used for jumping

    • @RootGames
      @RootGames  2 роки тому

      For another key, use "Keyboard.current.anotherKey". If you don't have errors try to use Debug.Log to see what is happening. Is collision between ray and objects happening, double-check ray length and collision layers, do objects have colliders and rigidbody2d components, etc...

  • @Oyasumi512
    @Oyasumi512 7 місяців тому

    it doesnt work please help me, no errors in code not sure whats going on.

  • @yaaaaaaaah
    @yaaaaaaaah 2 роки тому +1

    For anyone who has an issue with dropping the object, extend the box collider up. You are having an issue with dropping because they ray position is not detecting the box from the new position

  • @Nitrohedge21
    @Nitrohedge21 2 роки тому

    Thanks for the tutorial! I have a question though, the player can grab objects on both sides but can only drop them on the right side. I thought it was something to do with transform.right on the raycasthit2d line but i'm not sure if it is or how to change it.

    • @RootGames
      @RootGames  2 роки тому

      Depends on how you flip the player. If you use rotation for flipping the player, then the code will work. If you use the scale (-1), then instead of transform.right you need to put your variable that keeps the information of the player's direction. For example, direction=1 when the player is facing right, direction=-1 when facing right.

  • @JPeteMC
    @JPeteMC 2 роки тому

    Hi, I have a problem and I don´t know how to fix it. "NullReferenceException: Object reference not set to an instance of an object" Can you fix it for me? Thanks and great video!!😀

    • @RootGames
      @RootGames  2 роки тому

      Need more details about the error situation (when and where), but something is not assigned. Make sure that objects you are trying to grab have the Rigidbody2D component.

  • @LionelvsJapanTV
    @LionelvsJapanTV 2 роки тому

    My character is picking up the box, but isnt dropping it. please help

  • @Poiter0
    @Poiter0 Рік тому

    doens't work for me. if i try to grab the object it just runs away from me.

  • @OloskakBS
    @OloskakBS Рік тому

    i have imported the package but the "Keyboard" thing in the script is not green and it says that "Keyboard.CURRENT" the current thing is wrong
    can you help?

    • @RootGames
      @RootGames  Рік тому

      Make sure you use the namespace "using UnityEngine.InputSystem;"
      Also, you can try with Preferences->ExternalTools and click on the "Regenerate project files" button.

  • @amirshaide9545
    @amirshaide9545 Рік тому

    thanks but nothing worked and i have tried everything for the past 3 hours. Did you perhaps do something with the ray and grab child gameobjects?

    • @RootGames
      @RootGames  Рік тому

      No, everything is shown in the video. You need to use Debug.Log to determine the source of the problem.
      Also, maybe your ray origin is inside a collider and then it won't detect other colliders. Then you need to go to PlayerSettings->Physics2D and uncheck "Queries Start In Colliders"

  • @booppythegunner2350
    @booppythegunner2350 2 роки тому

    Thank you very much! But have some problem that if I grab a object, my character will run very fast and unstoppable, can you help me please?

    • @samirbaidon3472
      @samirbaidon3472 Рік тому +1

      Hi I have the exact same problem, have you been able to solve it?

    • @thisormaybethis
      @thisormaybethis Рік тому

      @@samirbaidon3472 try moving the grab point a bit further from your player

    • @samirbaidon3472
      @samirbaidon3472 Рік тому +1

      @@thisormaybethis Thanks at the end I managed to solve it, in fact it was by doing this

  • @insomnolentstudios
    @insomnolentstudios 11 місяців тому

    Whenever my player picks up the box he just starts moving automatically for no reason , any idea why?

    • @RootGames
      @RootGames  11 місяців тому

      Maybe the grab point is too close to the player, or something is wrong with rigidbody2d settings

  • @canofsoda
    @canofsoda Рік тому

    whenever i kill an enemy i can grab my tilemap and it turns into 1 block

    • @RootGames
      @RootGames  Рік тому +2

      Ensure your ray doesn't detect the layer you use for the tilemap.

  • @kikiob45
    @kikiob45 2 роки тому

    Why if my player touch the object, he suddenly dissappear? You know why?

    • @RootGames
      @RootGames  2 роки тому

      Maybe your grab point is too close to the player, which can lead to unwanted behavior. Also, make sure that the "rayPoint" is in front of the player's collider.

    • @kikiob45
      @kikiob45 2 роки тому

      @@RootGames Okay, Thank you Sir

  • @StickyLabDev
    @StickyLabDev Рік тому

    i hate new inputsytem

  • @bordexeditor5842
    @bordexeditor5842 2 роки тому

    My character isnt picking up the box, i have jump on my space so i did Keyboard.current.eKey.wasPressedThisFrame but it doesnt work either, can you help me please ?(btw i have fixed the moving platform,from the other tutorial)

    • @RootGames
      @RootGames  2 роки тому

      Try to debug it, for example : Debug.Log(hitinfo.collider) to see if your ray hits objects, also make sure that you type your layer correctly when getting the layer index from the name

    • @bordexeditor5842
      @bordexeditor5842 2 роки тому +1

      @@RootGames i had to set Physics2D.queriesStartColliders = false: so the ray wont stop in the collider, also i used the old Input system bcs that works as well