Search Unity

Raycast Not Interacting

Discussion in 'Scripting' started by Kyle_L, Mar 30, 2015.

  1. Kyle_L

    Kyle_L

    Joined:
    Feb 8, 2015
    Posts:
    9
    So 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<DoorScript>().doorInteractable; //Changes uiText Variable To RadioInteractable String
    interactableUI.GetComponent<UnityEngine.UI.Text>().text = uiText; //Changes UI.Text to uiText
    Interactable ();
    if (Input.GetButtonDown ("Interact")) {
    hit.collider.transform.parent.GetComponent<DoorScript> ().ChangeDoorState ();
    }
    }


    if (hit.collider.CompareTag ("RadioScene1")) { //If Interactable With Radio in Scene_1
    uiText = hit.collider.transform.parent.GetComponent<RadioScene1Script>().radioInteractable; //Changes uiText Variable To RadioInteractable String
    interactableUI.GetComponent<UnityEngine.UI.Text>().text = uiText; //Changes UI.Text to uiText
    Interactable ();
    if (Input.GetButtonDown ("Interact")) {
    hit.collider.transform.parent.GetComponent<RadioScene1Script>().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);
    }

    }