Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to script picking up stuff and inventory in 3d game

Discussion in 'Scripting' started by ShootingGamer, Jan 19, 2020.

  1. ShootingGamer

    ShootingGamer

    Joined:
    Nov 1, 2019
    Posts:
    5
    Hi. I am quite new in unity scripting and I have a lot's of ideas for games in my mind. I like concept of a game called raft and I want to make a survival game similar to that one but I can't find any tutorial how to script to pick up items only when you are looking at them and you are close enough (most of them are just shows scripts where you go to the item and you pick it automatically). I watched some tutorials about inventory but most of that stuff is quite confusing for me. I would appreciate any help.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    I don't even know how anyone could possibly respond usefully to this.

    I recommend you go back to the tutorial that seemed the easiest or closest to your problem (there are literally hundreds of inventory tutorials on Youtube!) and when you reach a point that you don't understand, use the Unity documentation to look up keywords in the code, go back over what the tutorial dude said, etc.

    When all that fails, come on back here, post the small snippet of code that you're having trouble with (remember code formatting! See first post in the forum) and your specific question, what you tried, etc., and we'd be happy to help.
     
  3. ShootingGamer

    ShootingGamer

    Joined:
    Nov 1, 2019
    Posts:
    5
    I will check video again and thank you so much for responding.
     
    Kurt-Dekker likes this.
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @ShootingGamer

    Some key things that might help - look into raycasting. Raycasting from screen (= your eyes) to world can be done using Camera's ScreenPointToRay method.

    Then you can use Vector3's Distance method to get distance to object.

    Here's one good tutorial on topic (IIRC):
     
    Kurt-Dekker likes this.
  5. BackgroundMover

    BackgroundMover

    Joined:
    May 9, 2015
    Posts:
    215
    Yeah I mean that to me is the un-fun part of programming, taking human-level concepts like "looking at" and "close enough" and turning it into code-level calls like Physics.Raycast() as eses linked. Some of the magic feels lost when you break things down into actual code.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    It's a little bit analogous to magic tricks in real life.

    Remember the first time you saw a really good magician make something disappear?

    Then you saw the Youtube showing how he did it?

    To me that isn't "magic lost" so much as it is "magic understood."

    Going back I remember the first time I saw PONG and saw the paddle hitting or missing the ball.

    To me that was magic.

    Almost immediately I got into game programming and as soon as I made my own two virtual objects hit and miss each other, it was like a whole new world opened. I had understood that magic.
     
    Yoreki, BackgroundMover and eses like this.
  7. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,492
    he should also probably look into these topic:
    1.vectors
    2.normal vectors
    3.dotproduct (of normal vector)
     
  8. ShootingGamer

    ShootingGamer

    Joined:
    Nov 1, 2019
    Posts:
    5
    Thank you so much for this replay I totaly forgot about raycast.