Problem is not reproducible or outdated
Raycast not working
Hey, the part with else if (hit.collider.gameObject.CompareTag("Key")) is't working. Is there another way to call the function in it? %|1666327396_1|% void Start () { } %|-845641456_6|% %|-332476484_7|% { if (Input.GetKeyDown(KeyCode.E)) { %|-1276657710_11|% RaycastHit hit; if (Physics.Raycast(ray, out hit, interactDistance)) %|-232198447_14|% if (hit.collider.CompareTag("Door")) { %|657757584_17|% if (doorScript == null) return; if (Inventory.Keys[doorScript.index] == true) { doorScript.ChangeDoorState(); %|-1771187766_22|% } else if (hit.collider.gameObject.CompareTag("Key")) { Inventory.Keys[hit.collider.GetComponent<Key>().index] = true; Destroy(hit.collider.gameObject); %|1793901035_28|% %|258592973_29|% } %|-2057044410_31|% }
Answer by GentleRaptor · Jan 27, 2016 at 09:29 AM
public class InteractScript : MonoBehaviour {
public float interactDistance = 5f;
void Start ()
{
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.E))
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, interactDistance))
{
if (hit.collider.CompareTag("Door"))
{
DoorScript doorScript = hit.collider.transform.parent.GetComponent<DoorScript>();
if (doorScript == null) return;
if (Inventory.Keys[doorScript.index] == true)
{
doorScript.ChangeDoorState();
}
}
else if (hit.collider.gameObject.CompareTag("Key"))
{
Inventory.Keys[hit.collider.GetComponent<Key>().index] = true;
Destroy(hit.collider.gameObject);
GetComponent<AudioSource>().Play();
}
}
}
}
}
The Script
Follow this Question
Related Questions
(Solved!) How to Destroy tagged items using raycast? 1 Answer
Scoring system not working. 1 Answer
function OnTriggerEnter using tags error 1 Answer
I need help with an IEnumerator. 2 Answers