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
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.
Thank you, and good luck with the project!
Thanks for sharing!
This is probably as beginner-friendly as it gets
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.
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*?
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.
Also, when I pick up an the box and look another direction, the box doesnt flip with my character. it stays on one side
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;
}
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;
}
}
}
Thx dude, I'll need It soon
Finnaly Its Working ! Thanks !
Does it need any components in GrabPosition and RayPosition to work?
ty for video, its working but i want to drag just 1 object,how can i do that?
the box is kinematic so it can go through other colliders, how do you fix that
Thank you,root!
Guys, if u have some problems with input system write me
😉
Thanks
dosent work for me whene i press space player grabed object but whene i pres again space dont want to release
are rayposition and grab position objects empy Objects?
Yes
I have a problem where it says that the keyboard does not appear within the context how do i fix this???
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.
Podrias utilizar un evento asociado a un Trigger "Contenedor" de espacio. Asi desligar de tu Player , una variable con el objeto. ¿no?. Excelente contenido!
CARAHLO EU NAO TINHA PENASDO EM FAZER ISSO
VERY USEFUL
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
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...
it doesnt work please help me, no errors in code not sure whats going on.
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
dont bro
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.
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.
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!!😀
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.
My character is picking up the box, but isnt dropping it. please help
doens't work for me. if i try to grab the object it just runs away from me.
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?
Make sure you use the namespace "using UnityEngine.InputSystem;"
Also, you can try with Preferences->ExternalTools and click on the "Regenerate project files" button.
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?
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"
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?
Hi I have the exact same problem, have you been able to solve it?
@@samirbaidon3472 try moving the grab point a bit further from your player
@@thisormaybethis Thanks at the end I managed to solve it, in fact it was by doing this
Whenever my player picks up the box he just starts moving automatically for no reason , any idea why?
Maybe the grab point is too close to the player, or something is wrong with rigidbody2d settings
whenever i kill an enemy i can grab my tilemap and it turns into 1 block
Ensure your ray doesn't detect the layer you use for the tilemap.
Why if my player touch the object, he suddenly dissappear? You know why?
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.
@@RootGames Okay, Thank you Sir
i hate new inputsytem
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)
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
@@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