This is the script:`
{
public static Item Instance;
public GameObject item;
public GameObject ItemHandSlot;
public Camera mainCam;
public static bool isPickedUp;
public bool isPicked;
public float range;
Rigidbody rb;
Collider cd;
RaycastHit hit;
// Start is called before the first frame update
void Start()
{
Instance = this;
ItemHandSlot = GameObject.Find("ItemHandSlot");
mainCam = Camera.main;
rb = GetComponent<Rigidbody>();
cd = GetComponent<Collider>();
}
// Update is called once per frame
void Update()
{
Ray ray = new Ray(mainCam.transform.position, mainCam.transform.forward);
Debug.DrawRay(ray.origin, ray.direction * range);
if(Physics.Raycast(ray, out hit, range))
{
if (hit.collider.gameObject == item)
{
if (!isPicked)
{
PickUp();
}
}
}
else
{
Drop();
}
if (isPickedUp)
{
item.transform.parent = ItemHandSlot.transform;
item.transform.position = ItemHandSlot.transform.position;
item.transform.rotation = ItemHandSlot.transform.rotation;
rb.useGravity = false;
cd.enabled = false;
}
else
{
item.transform.parent = null;
rb.useGravity = true;
cd.enabled = true;
}
}
public void PickUp()
{
if (!isPickedUp && Input.GetKeyDown(KeyCode.E))
{
isPickedUp = true;
isPicked = true;
}
}
public void Drop()
{
if (isPickedUp && Input.GetKeyDown(KeyCode.E))
{
isPickedUp = false;
isPicked = false;
}
}
}`
The problem I’m having is that when I pickup the item, I pickup all items that use this script.
Thank you for the help in advance.
Late with and answer as I don't have much time to develop a game, but I tried it and it didn't work... It still picks up all items using the same tag. I need to use the same tag as this game is a survival game so all food objects use "Food" Tag. (I've also tried instantiating the object but it didn't work.)
– Suren_Games