Search Unity

How do i make a grabbing system?

Discussion in 'Scripting' started by MrDiamondDog, Sep 16, 2020.

  1. MrDiamondDog

    MrDiamondDog

    Joined:
    Aug 12, 2020
    Posts:
    20
    I want to make a grabbing system for my game, and i've looked everywhere for a tutorial, but they either are old and don't work, or for VR. I do not want a grabbing system for VR, just one that is like E to pick up an object in a certain layer. Can someone help me please?
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    I'm assuming that the actual problem was that you looked for "grabbing system", which can mean lots of things and also explains all of your VR results. Instead look for "Unity pick up item", "Unity interact with item" or something along those lines, which should give plenty of results and tutorials for what you need.

    The basic idea is to cast a ray on button press, and see if it collides with a collectable item. As you require an inventory to pick up items into, i imagine the major part of any tutorial you find will cover inventories as well.
    https://docs.unity3d.com/ScriptReference/Physics.Raycast.html
     
    Last edited: Sep 16, 2020
  3. MrDiamondDog

    MrDiamondDog

    Joined:
    Aug 12, 2020
    Posts:
    20
    Thank you, but i want a system that lets you pickup an object and move it around, not an inventory system.
     
  4. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    Ye well,

    the principe is the same as with an Inventory Item which you want to pickup,
    the only difference is that you dont want to destroy the pickable item but drag it around,
    not much of a difference there and the principe will be the same


    Thats what you need:
     
  5. MrDiamondDog

    MrDiamondDog

    Joined:
    Aug 12, 2020
    Posts:
    20
    again, this is not what i am looking for.

    i do not know how to make it move on my own, that is why i am asking this question.
     
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    This depends a lot on how the interaction is supposed to feel, or how the item is supposed to behave while in air.

    The easiest way to move it around would be to add it as a child to your character model. This has the disadvantage that the movement feels like as if "on rails", and depending on your setup you may be able to move it through objects.
    Another approach is to use teleport the object to a new location each frame, such that it remains in the same distance it was when picked up - or not if you want it closer. You can use a raycast to check if there is an object closer than that and if that's the case drop the item, or pull it in front of that obstacle.
    Or you could get fancy and apply forces to the object after interacting with it, pushing it towards some position you want to have it at. As i said, it depends on how the item interaction is supposed to "feel" after you picked the item up.

    I would probably go with the teleport approach as i feel like it gives me the most control programmatically. Either way you should start with the basics and get your raycast set up. Get to the point that you recognize the item on a key press (if it is in front of you). Then define a position relative to your character in which you want the picked up item to be.. and make it go there.
     
    MrDiamondDog and Lethn like this.
  7. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    The way I've done mine is I've treated it like an FPS almost where you 'activate' carried objects, you destroy the object you've just picked up but as far as the player is concerned they're carrying it. After that you can instantiate your gameobject and throw it with addforce or something then deactivate the gameobject where you're holding the object. It all happens so fast the player isn't likely to notice, even if does you can probably transition it fairly easily with an animation of some kind.
     
    MrDiamondDog likes this.
  8. MrDiamondDog

    MrDiamondDog

    Joined:
    Aug 12, 2020
    Posts:
    20
    can you please help me with the equation? i made it so the block rotates with the camera when you click 'e' on it and change position, but i can't figure out an equation to put it in front of the camera. can you please help?
     
  9. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    It's not an equation, look up transform.position.
     
  10. MrDiamondDog

    MrDiamondDog

    Joined:
    Aug 12, 2020
    Posts:
    20
    well, if i want the block to stay in front of the camera, i would need an equation to calculate the position the block needs to be.
     
  11. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Since, based on previous replies, you'd pick the item up after it is in front of you and you look at it, the item is always in for players' forward direction (transform.forward). You get the distance it was originally at from the first raycast (hit.distance). So basically, if you do not want to change the position of the object and keep it at the same relative position to your character, that position would be your forward vector multiplied with the distance it had on pickup. You may need to add an offset, since the position the ray hit on that object may vary slightly from the actual position of the object. This offset is simply otherObject.transform.position - hit.position (calculated on pickup).

    If you want to pick up the item and hold it in your hand, just to what @Lethn suggested. From your explanation i still feel like you want the item to somehow float around in front of you. I just cant imagine how that's a proper game mechanic. Maybe you have an example of a game that uses this (and potentially a video for reference)?
     
  12. MrDiamondDog

    MrDiamondDog

    Joined:
    Aug 12, 2020
    Posts:
    20
    thank you, but i already finished the grabbing system and i would not like to edit it. I am just making a personal game for fun, i am not releasing this anywhere except for maybe a few of my friends. here is a video if you are interested: