I want take item if I am bringing rear sight and press key E.
I add trigger for Item
public void OnTriggerStay(Collider other){
if (other.tag == "Player"){
Inventory.GetInventory().TakeItem();
}
}
public void TakeItem(){
RaycastHit hit;
Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
if (Input.GetKeyDown (KeyCode.E)) {
if (Physics.Raycast(ray, out hit)){
Item item = hit.transform.GetComponent<Item>();
if (item != null){
item.audioTakeItem.Play();
items.Add(item);
item.gameObject.SetActive(false);
}
}
}
}
But this code wrong. I can take item if rear sight in not directed item.
Sorry my wrong English =)