Search Unity

Using Raycasting to open doors

Discussion in 'Scripting' started by loafsick, Oct 9, 2019.

  1. loafsick

    loafsick

    Joined:
    Apr 20, 2015
    Posts:
    5
    Hi Everybody!

    So, I'm a little confused about a basic concept here. I've watched numerous tutorials about opening doors (or just playing animation clips) using raycast, but most of them are javascript, and that doesn't seem to be supported any longer. I am new to C#, and have little to no understanding of JS, so I need a C# solution. I have my reticule already in my UI, and it'll just stay on screen, always. The raycast is emanating from the camera on my FPS (and the script is attached to the camera). I have box colliders on each door. I have the "door" tag applied to each door. Basically, when the door to be opened is in range of the raycast, and I click "w", the door open animation will play, and when it is out of range, the door will close (I don't know how to write this part). I know that this script is incomplete and there is an error regarding the 'input.getkeydown' line (although that works in other scripts, for some reason). Can somebody lead me out of the wilderness?

    Thanks in advance,

    Stephen

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RaycastShootALT : MonoBehaviour
    6. {
    7.     public Vector3 point = new Vector3(0.5f, 0.5f, 0);
    8.  
    9.     Camera cam;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.      
    15.         cam = GetComponent<Camera>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.        
    22.         Ray ray = cam.ScreenPointToRay(new Vector3(0.5f, 0.5f, 0));
    23.         RaycastHit hit;
    24.         if (Physics.Raycast(ray, out hit))
    25.             if (hit.transform.gameObject.tag == "Door")
    26.                 if (Input.GetKeydown("w"))
    27.  
    28.                 {
    29.                     hit.collider.gameObject.GetComponent<Animation>().Play("DoorOpen");
    30.                 }
    31.  
    32.         }
    33.  
    34.        
    35.    
    36. }
    37.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    In the future, tell us what the error is - errors really do contain useful information. In this case, I can tell you the issue is that you forgot to capitalize the "D" in "Down".

    The only other real issue that jumps out at me with this script is that your Ray creation line is going to be casting a ray towards nearly the top left corner of the camera's view - ScreenPointToRay takes pixel coordinates. Your should use ViewportPointToRay if you want to use normalized coordinates (e.g. 0.5 = middle). If you're just trying to shoot straight forward, you could also use this to create the ray:
    Code (csharp):
    1. Ray ray = new Ray(cam.transform.position, cam.transform.forward);
     
  3. loafsick

    loafsick

    Joined:
    Apr 20, 2015
    Posts:
    5
    Sorry about the lack of clarity. So, I had tried ViewportPointToRay and it gave me an error. I forgot what it was, and have moved on. However, the issue with this script is that it does nothing when all the criteria is met. I replaced the old 'ray' line with your option (so I assume that the ray is now firing right down the center of the screen, which is what i want). However, lining up my target door in the center of the screen, and clicking "m" (had to replace 'w' because it is used for the FPS movement), does not play the "DoorOpen" animation clip. In fact, I see nothing in the console telling me that it can't find that clip, or some other syntax error. I guess that my question is more about the conditional portion of the script. Am I phrasing all of that correctly? And, this line:

    hit.collider.gameObject.GetComponent<Animation>().Play("DoorOpen");

    Is that correct?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    This suggests that it's not failing at the task, but rather that it's not attempting it. Put Debug.Log's in various places and see which ones get printed out - one inside the raycast "if" statement, one inside the tag check, one inside the keyboard input check. That way you'll know exactly what is and isn't coming back "true".

    Ah, I didn't catch it, but no, it isn't. It probably was correct several years ago when the tutorial was made, but the "Animation" component is obsolete (it became obsolete not long after Javascript fell out of common usage). The current animation system uses the Animator component, and it works a little differently. Check out tutorials on that component (like this) for more information.

    (This line should be throwing a NullReferenceException because the object wouldn't have the Animation component; the fact that it's not means that it isn't getting executed, as described in the top paragraph of this comment)
     
  5. loafsick

    loafsick

    Joined:
    Apr 20, 2015
    Posts:
    5
    Interesting! When I changed 'Animation' to 'Animator', it worked! No need to debug for now. Thanks again!