- Home /
Help with "inventory" system c#
Im making an pickup objects system, in which the player should be able to pick up an item with a specific tag and when it is picked up, something will happen ex. when u pick up food your health gets higher.
here is my code;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class pickup : MonoBehaviour
{
float maxDistance = 20;
public GameObject target;
void FixedUpdate()
{
// Will contain the information of which object the raycast hit
RaycastHit hit;
// if raycast hits, it checks if it hit an object with the tag tag
if(Physics.Raycast(transform.position, transform.forward, out hit, maxDistance) && hit.collider.gameObject.CompareTag("tag") && Input.GetKey (KeyCode.E))
{
target.SetActive (false);
}
}
}
and the thing that can be picked up will have a code that runs when the object is deactivated. My problem is that right now i can just have one object that can be picked up (the target), id like to have it so that anything with that tag, or any child in a game object can be picked up without influencing the other objects in that game object. Please answer! P.S. Sorry for bad english.
I didn't understand ur issue Can u please elaborate!!
Answer by x4637x · Aug 23, 2017 at 05:35 AM
If I understand you correctly, Physics.SphereCastAll or Physics.RaycastAll should help you on that.
@x4637x how do u use that? Could you please implement it in my code?
@daleonard First of all you should use comment for reply. Also next time you trying to paste your code here, you could select them all and do "Ctrl+$$anonymous$$" to format them.
Secondly RayCastAll is basically the same as RayCast, only different is it will return a list of hit as RaycastHit, in this case they are all the targets you want to pick up. Use a for loop or foreach loop to do things to them individually then you should be able to pick them all up.
Here is the code :
// Will contain the information of all objects hit
RaycastHit[] hit = Physics.RaycastAll(transform.position, transform.forward, maxDistance);
//filter out non-interest objects
List<GameObject> targets = new List<GameObject>();
for (int i = 0; i < hit.Length; i++) {
if (hit[i].collider.gameObject.CompareTag("tag")) {
targets.Add(hit[i].collider.gameObject);
}
}
//do pick up if there is any object in the list
if (targets.Count > 0 && Input.Get$$anonymous$$ey($$anonymous$$eyCode.E)) {
for (int i = 0; i < targets.Count; i++) {
//do your pick up function here
}
}
So the only thing that defines what can be added into the list is the tag?
Yes. But I suggest you using layer ins$$anonymous$$d. Is faster then getting all game object from raycast hit and then filter out unwanted game objects.
You could read up on Physics.RaycastAll about usage of Layer and Layer$$anonymous$$ask at the parameter section.
O$$anonymous$$, thank you so much, but i still got one question, what is the "do ur pickup funktion here", what is the "target", and how does the code know which target im looking at?
1) That is telling you insert whatever you have for handling the actual "pick up". Some code that will move the object into your player's inventory. I assume you already have something to do that part of job?
2) If you mean the "targets" in for loop, that is the list of game objects that could be pick up in front of your player (assu$$anonymous$$g your player need to face the object in order to pick it up).
3) The code knowing where you lookling at by doing the "RaycastAll", then it gather all game objects along that direction within "maxDistance". If you don't want to pick them all up at once, you could handle it in last if statement. Something handle single item selection from the "targets" list will do the job.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Item Database / Dictionary 1 Answer
Creating an inventory system c# 0 Answers