C# Raycast Hit open door and true false Bool questions

I have a scene that I am trying to click on collision boxes to interact with like opening a door and bouncing a ball for tests. I have this working, kind of. My questions are:

With the GetButtonDown code, it only allows me to click on the edge of the collision object, like it only accepts the original hit, so in my Oculus I have to keep hitting the A button and moving my head until it actual plays the animation. The reticle now stays on until it hits something else, with the code I have below, but before that would also only stay on while it originally hit the edge of the collision detection. Now I have it set to stay on until it hits something else, so it seems this is what the GetButton code is doing. How do I set this to see the collision the whole time so it is a more reliable hit, so I don’t have to look around on the object smashing the A button until it plays the animation?

My second question is with the true false bool. I can print this bool and in the console it shows a capital False or True (whatever I set in the intial bool). But it only plays the door close animation, so basically always thinks it’s true and doesn’t switch states either.

Can anyone please help me with these 2 issues?

using UnityEngine;
using System.Collections;

public class Reticle : MonoBehaviour {
    public Camera CameraFacing;
    public Texture2D CrosshairTexture;
    public Texture2D UseTexture;
    public Texture2D NormalTexture;
    private Vector3 originalScale;

    public bool isOpen = false;
   
    public GameObject ballanimate;
    public GameObject Dooranimate;

 
    void Start () {
        originalScale = transform.localScale;
    }
   

    void Update () {
        RaycastHit hit;
        Ray reticle = new Ray (transform.position, Vector3.down);
        float distance;
        if (Physics.Raycast (new Ray (CameraFacing.transform.position,
                                      CameraFacing.transform.rotation * Vector3.forward),
                             out hit)) {

                if(hit.collider.gameObject.tag == "Ball_Collide")
                {
                    this.gameObject.GetComponent<Renderer>().material.mainTexture  = UseTexture;
         
                    if(Input.GetButtonDown("Button A"))
                    {
                        ballanimate.GetComponent<Animation>().Play("BallBounce");
   
                    }
                   
                }else if(hit.collider.gameObject.tag == "Door_Animate")
                {
                    this.gameObject.GetComponent<Renderer>().material.mainTexture  = UseTexture;
                    //guiShow = true;
                    if(Input.GetButtonDown("Button A") && isOpen == false)
                    {
                        isOpen = true;
                        Dooranimate.GetComponent<Animation>().Play("Door_Anim");
                       
                    }
                   
                    if(Input.GetButtonDown("Button A") && isOpen == true){
                        isOpen = false;
                        Dooranimate.GetComponent<Animation>().Play("Door_Anim_Close");
                       
                    }
                   
               

            }else if(hit.collider.gameObject.tag == "Ground_Collision"){
                this.gameObject.GetComponent<Renderer>().material.mainTexture  = NormalTexture;
            }
            distance = hit.distance;

        } else {
            distance = CameraFacing.farClipPlane * 0.95f;
            this.gameObject.GetComponent<Renderer>().material.mainTexture  = NormalTexture;
        }
        transform.position = CameraFacing.transform.position +
            CameraFacing.transform.rotation * Vector3.forward * distance;
        transform.LookAt (CameraFacing.transform.position);
        transform.Rotate (0.0f, 180.0f, 0.0f);
        if (distance < 10.0f) {
            distance *= 1 + 5*Mathf.Exp (-distance);
        }
        transform.localScale = originalScale * distance;

        }
    }

For issue #1, what you probably want is to constantly cast at the collider in your scene, and then when you successfully hit it, display a prompt saying “Press A to open the door” as well as enable pressing A to actually open the door.

For issue #2 look into the Unity animation controller objects because then you can set a two-state animation machine so that it travels from open to closed based on the contents of an isOpen boolean, which you would change directly from your script.

Yep Kurt, I do have 2 animations and have them seperated on the door, one is called Door_Anim and Door_Anim_Close respectively, which is what the script is calling, and it calls the close animation, but only calls that one wether the bool (isOpen) is set to true or false. That’s the question I have, how do I fix that?

And for the first part, my rasycast is constantly calling to the collider, and just as you say have a prompt display, I have the reticle change to a larger brighter yellow reticle, same concept, just showing it is clickable, but it doesn’t click even tho the reticle changes unless I keep hitting A continuously and move the reticle around, normally only works on the edge of the collider, like it only works on the intial hit state.

But how do I fix both the issues? You gave me the solutions to exactly what I have working in the script and how I have everything setup. :slight_smile: