I’m making an inventory script for my game, and I would like to make it so, when a player igname looks at an item a “press button to pick up” text appears and the item gets higlighted
- Use an “outline” asset (there are free ones) that draw an outline
- Swap your materials to a highlight material, such as emissive transparent
The term “highlight” is gonna be completely different in the inventory versus in the game. Choose which you’re talking about and look up ways that apply to your situation.
Keep this in mind too, just to know the sheer scope of what you’re dealing with here from a data processing standpoint:
These things (inventory, shop systems, character customization, dialog tree systems, crafting, etc) are fairly tricky hairy beasts, definitely deep in advanced coding territory.
Inventory code never lives “all by itself.” All inventory code is EXTREMELY tightly bound to prefabs and/or assets used to display and present and control the inventory. Problems and solutions must consider both code and assets as well as scene / prefab setup and connectivity.
Inventories / shop systems / character selectors all contain elements of:
- a database of items that you may possibly possess / equip
- a database of the items that you actually possess / equip currently
- perhaps another database of your “storage” area at home base?
- persistence of this information to storage between game runs
- presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
- interaction with items in the inventory or on the character or in the home base storage area
- interaction with the world to get items in and out
- dependence on asset definition (images, etc.) for presentation
Just the design choices of such a system can have a lot of complicating confounding issues, such as:
- can you have multiple items? Is there a limit?
- if there is an item limit, what is it? Total count? Weight? Size? Something else?
- are those items shown individually or do they stack?
- are coins / gems stacked but other stuff isn’t stacked?
- do items have detailed data shown (durability, rarity, damage, etc.)?
- can users combine items to make new items? How? Limits? Results? Messages of success/failure?
- can users substantially modify items with other things like spells, gems, sockets, etc.?
- does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
- etc.
Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.
Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.
Breaking down a large problem such as inventory:
If you want to see most of the steps involved, make a “micro inventory” in your game, something whereby the player can have (or not have) a single item, and display that item in the UI, and let the user select that item and do things with it (take, drop, use, wear, eat, sell, buy, etc.).
Everything you learn doing that “micro inventory” of one item will apply when you have any larger more complex inventory, and it will give you a feel for what you are dealing with.
Breaking down large problems in general:
I’m sorry if you misunderstood me but I meant so whenever I’m ingame and I’m looking for an item that I can pick up whenever I’m looking at it. It highlights and a “button to pick up” text appears.
Since you still haven’t posted any details about if your game is 2D, 3D, sprite-based or anything else, I urge you to hurry over to Youtube for any one of the five million hits on this well-studied well-documented process.
Okay, but that doesn’t say anything about your requirements. Highlighting can be anything. It could be particles that appear around the item, it could be an outline around the item, the item itself could change colors, etc. You need to specify because all of these are implemented differently.
I’m assuming you want to highlight something in 3D space and you’re using some FPS controller of some sort. My approach for this is a bit like this:
- Put things you can highlight on a seperate layer, called “interaction”. This layer will contain all the switches and other interactable stuff in the environment.
- Have a constant raycastAll() run from your camera position towards the direction of your camera.transform.forward using a LayerMask that only has the layer “interaction” set. The result is an array of objects on that layer, which that ray intersects. Have max distance be a logical number (this represents the max distance from which you can interact with stuff, basically the length of your arms).
- If this array is longer then 0 then we have hit something. The first item in this list is the closest.
I just store the first result in a variable (lets call it myInteractable) of a component on the player (let’s call it MyPlayer). When your variable was empty and you find an object, highlight the object. When your variable is filled but you do not, un-highlight the object in the variable and set it to null.
I also use a component class called Interactable, that has a Highlight and Unhighlight method. Every object you can interact with extends this class. When that raycasting method finds an object, I call GetComponent() on it (once!) to find the component that extends Interactable, so these methods can be fired when appropriate. Instead of a GameObject, I store a reference to this Interactable component so it’s public methods can be fired.
The Interactable class also has a public virtual method called Interact, which subclasses can override. MyPlayer can then fire this method when it’s variable myInteractable is set and some input is given (for example Input.GetButtonDown(“Fire1”)), with each subclass of Interactable (InteractableSwitch, InteractablePickup) being able to produce a different result.
Hey I think I found what I needed thanks for help and sorry for my bad explaining.