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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Carrying and Dropping objects help

Discussion in 'Scripting' started by BitRei, Jul 27, 2018.

  1. BitRei

    BitRei

    Joined:
    Jan 15, 2018
    Posts:
    69
    I'm making a game where you play as a harpy mailman. Now this means part of the game is lifting up larger packages and carrying them while flying. I've got the flying down, but I'm having an issue with with picking up other objects. I'm thinking I'd need to make the object a child of the player. There isn't an inventory or anything that the object would go in, and it's a 2D game. Help?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Well, you have an idea, just curious, but did you try it out?

    Chances are you'd want to make it a child and then possibly position it so it's in the characters hands. Or, you could even just have a sprite that already has the package in hand and swap sprites when the character picks something up. Or, even have a package already in place as a child and turn that on and off as needed.

    There are many ways to do things and you'll just need to choose one that works for you, but certainly don't be afraid to try things out.
     
    Joe-Censored likes this.
  3. BitRei

    BitRei

    Joined:
    Jan 15, 2018
    Posts:
    69
    I did. I would make a sprite with the package in hand, but I'm wanting to make mechanics where if you hit a wall you drop it, as well as just straight up dropping it to deliver. I'm still somewhat new to Unity too.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Take these issues one at a time instead of trying to do it all at once. As already mentioned, to have your character carry an object the simplest way is to parent the object to the character and position it relative to the character.

    Dropping an object is usually as easy as making the object no longer a child of the parent and letting rigidbody physics do the work of having it fall with gravity.

    Detecting a hit against a wall is generally done using colliders. You detect a collision, you verify what you collided with is something you are interested in (check the tag assigned to the gameobject that you collided with for example), and then do something in response. That something you do could then be to call your drop object method.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    Not sure if you want to use real physics to carry your payload, like suspended from your player, but if you want an example, check this:

    https://bitbucket.org/kurtdekker/jiggly_lander

    It's a little jiggly-legged lander that can also pick up payloads, which swing around as if on a rope as you try to fly. It uses some kind of hinge to connect it; I forget the details but it's all in the code.
     

    Attached Files:

  6. BitRei

    BitRei

    Joined:
    Jan 15, 2018
    Posts:
    69
    Ok. Thanks for the help, after a little while I got it and it works great now, all it needs is polishing. :D