The problem I’m facing is…
Periodically when I ‘pick up’ a gameobject, and other objects i can also ‘pick up’ are quite close to each other, it will pick up both items up, even though the raycast is only hitting one at a time.
```csharp
**using UnityEngine;
using System.Collections;
public class PickUp : MonoBehaviour {
public LayerMask interaction;
public bool canPickup; // keep public for testing
public Inventory inv; // keep public for testing
private Item item;
/// Need to fix up picking up objects (believe raycast isnt working properly)
/// Within this block of code
private void Update () {
if (canPickup && inv != null) {
Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit h; // Using for testing purposes
if (Physics.Raycast(r, out h, 100f, interaction, QueryTriggerInteraction.Ignore)) {
Debug.DrawLine(r.origin, r.GetPoint(100), Color.blue);
Debug.Log(h.transform.name);
if (Input.GetKeyDown(KeyCode.E)) {
inv.pickUpObject(SwitchState());
}
}
}
}
// Add a type for each class inherited from Item
private Item SwitchState () {
System.Type t = GetComponent(gameObject.tag).GetType();
Item i = null;
// Type Checking
// Type Gun
if (t == typeof(Gun)) {
i = gameObject.GetComponent<Gun>();
}
// Type Sword
else if (t == typeof(Sword)){
i = gameObject.GetComponent<Sword>();
}
return i;
}
private void OnTriggerEnter (Collider other) {
if (other.CompareTag("Player")) {
canPickup = true;
inv = other.GetComponent<Inventory>();
}
}
private void OnTriggerExit (Collider other) {
if (other.CompareTag("Player")) {
canPickup = false;
inv = null;
}
}
}**
```
An image of the object setup…