- Home /
Trigger and raycast error?
Basically im starting an inventory script. I first tried using a raycast, which detects what object its looking through a tag, then returns the name of the gameObject. however, sometimes the ray doesnt pick up the ray. This happens as well while using a trigger. Everything within the code works once the ray can detect the object. The object being detected has a rigidbody component. heres the script:
#pragma strict
var selectedObjectName : String;
var mainCamera : Transform;
var cameraObj : Camera;
var rayDistance : float = 10;
var selectedObject : GameObject;
var rayOn = false;
var guiBackground : Texture;
//GUI positions
var guiX : float = 200;
var guiY : float = 200;
var guiHeight : float = 250;
var guiWidth : float = 250;
function Update () {
var hit : RaycastHit;
var ray = cameraObj.ScreenPointToRay(Vector3(Screen.width/2, Screen.height/2, 0));
Debug.DrawRay(ray.origin, mainCamera.transform.forward * rayDistance, Color.red);
if(Physics.Raycast(ray.origin, ray.direction * 15, hit, rayDistance))
{
else if(hit.collider == null)
{
rayOn = false; // ray is off
}
if(hit.collider.gameObject.tag == "collectable") // if object with tag collectable is detected
{
rayOn = true; //then ray is on <--- this is for checking whether ray is detecting object or not
if(Input.GetMouseButtonDown(0))
{
selectedObjectName = hit.collider.name;
Debug.Log("the selected object is called " + hit.collider.name);
var selectedObject = GameObject.Find(selectedObjectName); // check for string name of current object
Debug.Log("the actual object is " + selectedObject.name); //check to see if it was changed to gameobject
}
}
}
}
paste your code in from your selected coding application into the window, then highlight all of it and press the code button, which is the button that has "101010"
Answer by dannyskim · Jan 18, 2012 at 09:16 PM
if(Physics.Raycast(ray.origin, ray.direction * 15, hit, rayDistance))
{
if(hit.collider.gameObject.tag == "collectable") // if object with tag collectable is detected
{
rayOn = true; //then ray is on <--- this is for checking whether ray is detecting object or not
if(Input.GetMouseButtonDown(0))
{
selectedObjectName = hit.collider.name;
Debug.Log("the selected object is called " + hit.collider.name);
var selectedObject = GameObject.Find(selectedObjectName); // check for string name of current object
Debug.Log("the actual object is " + selectedObject.name); //check to see if it was changed to gameobject
}
}
}
else
rayOn = false;
this is what i meant
THAN$$anonymous$$S A BUNCH FOR YOUR TI$$anonymous$$E! it worked. now i understand it better. thanks for sticking around.
P.S if you can help with a GUI issue, it would be much appreciated. im posting the new question now
Answer by dannyskim · Jan 18, 2012 at 08:09 PM
var selectedObject = gameObject.Find(selectedObjectName);
Try changing the 'g' on gameObject to a capital G. So:
var selectedObject = GameObject.Find(selectedObjectName);
I'll see if I can spot anything else in the meantime while you try that out.
thanks but thats not the problem. The meat of the code works, the issue is with the raycast hit itself, as it wont detect the object (collectable object in this case). It SO$$anonymous$$ETI$$anonymous$$ES detects the object, but not everytime.
are you sure your distance variable doesn't need to be increased for the raycast? Take for example if it's casting from the camera position and it's not casting directly in front of it, but at a diagonal, the distance will increase.
okay i centered the ray using screen pointToRay nstuff. The code should be updated. I have noticed now that the ray will detect the object towards the center of it, and the rayOn variable turns false whenever the ray interacts with another collider. HOWEVER it doesnt turn false when it touches nothing. How can i tell it to turn false when it touches nothing?
ahh I'm sorry, simple error on my part. When you take a look at your if( Physics.Raycast...) statement, that is technically checking if the physics.raycast returns true. When you look at the documentation, Physics.Raycast returns a boolean value of true or false. So you simple need to put the else statement after the if( Physics.Raycast) block to turn rayOn to false;