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 boolfor isOpen. 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?
I'm using Unity 5
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 boolfor isOpen. 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?
I'm using Unity 5
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;
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;
}
}
