Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Raycast is not being detected. (Problem Solved!)

Discussion in 'Scripting' started by RiseBestgirl, Jan 12, 2015.

  1. RiseBestgirl

    RiseBestgirl

    Joined:
    Dec 17, 2014
    Posts:
    10
    My ray casts are clearly being shot out when I go into the game but the problem is that it's not detecting the items I've tagged as Key or Door when it casts them. the key doesn't get destroyed and the door doesn't open.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Keys : MonoBehaviour {
    5.  
    6.  
    7.     public float maxKey = 5;
    8.     public float curKey = 0;
    9.  
    10.     void  Update (){
    11.         if(Input.GetKeyDown(KeyCode.E)){ //If E is pressed
    12.             Debug.Log("Pressed");
    13.             RaycastHit hit;
    14.             Vector3 fwd = transform.TransformDirection(Vector3.forward);
    15.             if (Physics.Raycast (transform.position, fwd, out hit, 10)) {// Sends out a raycast to check if something is in front of you
    16.                 Debug.DrawRay(transform.position, fwd * 10, Color.green);
    17.                 if(hit.transform.Equals("key")){ // If the object in front of you has the tag Key
    18.                     Debug.Log("the Key Is There");
    19.                     curKey += 1;
    20.                     Destroy(hit.transform.root.gameObject);//Destroy Key Object
    21.                     animation.Play("Door");
    22.                 }else if(hit.transform.name.Equals("Door")){ //Checks if the gameobject you're looking at has the tag Door
    23.                     if (curKey == 1)
    24.                         Debug.Log("HasKey");
    25.                     animation.Play ("New Animation"); //Calls the function Unlock on the door
    26.                     curKey -=1;
    27.                 }
    28.             }
    29.         }
    30.     }
    31. }
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,936
  3. RiseBestgirl

    RiseBestgirl

    Joined:
    Dec 17, 2014
    Posts:
    10
  4. Deleted User

    Deleted User

    Guest

    Instead of:
    if(hit.transform.Equals("key"))

    Try:
    if (hit.transform.gameObject.tag == "Key")
     
  5. RiseBestgirl

    RiseBestgirl

    Joined:
    Dec 17, 2014
    Posts:
    10
    That worked super well thanks.
     
    Haseeb_BSAA likes this.
  6. RiseBestgirl

    RiseBestgirl

    Joined:
    Dec 17, 2014
    Posts:
    10
    Okay so now the key is picked up and added to the value but the animation for the door opening does not play. Do I need to create a new script that calls to this one to open the door or is there still something wrong with this one. I changed
    Code (CSharp):
    1. (hit.transform.name.Equals("Door")
    to
    Code (CSharp):
    1. }else if(hit.transform.gameObject.tag == "Door"){
    .
     
    Haseeb_BSAA likes this.
  7. Deleted User

    Deleted User

    Guest

    Are you sure the name of the animation is New Animation? animation.Play("New Animation");
     
  8. RiseBestgirl

    RiseBestgirl

    Joined:
    Dec 17, 2014
    Posts:
    10
    Yes, it was called "door" at first but I changed it to "New Animation" for the sake of testing the script.
     
    Last edited: Jan 13, 2015
  9. skusku

    skusku

    Joined:
    Jan 4, 2015
    Posts:
    17
    You are calling animation.Play("New Animation");
    Shouldn't you be calling something like "hit.transform.gameObject.animation.Play("New Animation"); ?
     
  10. RiseBestgirl

    RiseBestgirl

    Joined:
    Dec 17, 2014
    Posts:
    10
    The doors now work. thanks for your help.
     
    skusku likes this.