- Home /
Question by
Kyle_L · Apr 14, 2015 at 11:06 PM ·
physicsraycastscriptingbasicsglitch
Raycast not interacting!
Ok i have a raycast from my main camera checks to see if it hitting any object with a specific tag, and when it hits enables a UI element. Everything works fine except it doesn't detect the tagged object unless its positions negative on the y axis, and then it detects every below and above the object as the object if it does detect the object.
using UnityEngine;
using System.Collections;
public class Interact : MonoBehaviour {
public GameObject cursor;
public GameObject interactCursor;
public GameObject interactableUI;
//Player's Distance to Door
public float distance = 5f;
private string uiText;
void LateUpdate () {
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
NotInteractable();
if (Physics.Raycast (ray, out hit, distance)) {
if (hit.collider.CompareTag ("Door")) { //If Interactable With Door
uiText = hit.collider.transform.parent.GetComponent().doorInteractable; //Changes uiText Variable To RadioInteractable String
interactableUI.GetComponent().text = uiText; //Changes UI.Text to uiText
Interactable ();
if (Input.GetButtonDown ("Interact")) {
hit.collider.transform.parent.GetComponent ().ChangeDoorState ();
}
}
if (hit.collider.CompareTag ("RadioScene1")) { //If Interactable With Radio in Scene_1
uiText = hit.collider.transform.parent.GetComponent().radioInteractable; //Changes uiText Variable To RadioInteractable String
interactableUI.GetComponent().text = uiText; //Changes UI.Text to uiText
Interactable ();
if (Input.GetButtonDown ("Interact")) {
hit.collider.transform.parent.GetComponent().Play();
Destroy(hit.collider);
}
}
}
}
//Changes Cursor to InteractableCursor
void Interactable () {
interactCursor.SetActive(true);
cursor.SetActive(false);
interactableUI.SetActive (true);
}
//Changes InteractableCursor to Cursor
void NotInteractable () {
interactCursor.SetActive(false);
cursor.SetActive(true);
interactableUI.SetActive (false);
}
}
Comment
Your answer