cancel
Showing results for 
Search instead for 
Did you mean: 

C# Raycast Hit open door and true false Bool questions

ebolt76
Honored Guest
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

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;

}
}


1 REPLY 1

wheatgrinder
Explorer
I've been wanting to replay, but Iv not tried your code yet.

Right off I would suggest turning your code upside down.

By that I mean. You should have your objects respond to the ray cast hit appropriately rather than have the code that is sending the ray cast do the work. Think of it as "Keeping my actions inside myself" to me it is easier to deal with. This is also way easier to debug. You can just write a script to test your actions THEN work on why your raycast isnt triggering the action.

Your ray cast code simply invokes some default procedure the colliders parent object when the reticle hits it. You can use layers or tags to set it so that your ray only triggers the default action on certain objects.

People say "sendmessage" is bad for this and there are better methods, but it woks for me. Using send message is very easy and you can even tell it to ignore the error that occurs if there is no "listener".. (send message options = DontRequireReceiver)

In my raycasters code I use something like:
On Button Down (A) hit.collider.sendmessage("Fire1")

Every object that I want to respond to ray cast and A button press has a "Fire1()" function. That function does the action..

hope this is helpful.