- Home /
Make object block a raycast
I am using raycast to check for touches on object, however the problem I have is that the raycast is passing thru walls and activating the touch code on objects on the other side of the wall. For example on one side of the wall there is a torch and on the other is a chest. WWhen player touches the torch the chest on the other side opens.
Is there a way to stop raycasts from passing thru certain objects?
On Torch objects the code is
function Update ()
{
if (Input.GetMouseButtonDown(0)) // check for left-mouse
{
if (torchLit == false){
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (collider && collider.Raycast (ray, hit, 100.0))
{
if(hit.collider.tag == "Torch"){
OnMouseDown();
}
}
}
}
}
And on my chest object
function Update () {
if (Input.GetMouseButtonDown(0)) // check for left-mouse
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (collider && collider.Raycast (ray, hit, 100.0))
{
if(hit.collider.tag == "chest"){
OnMouseDown();
}
}
}
if (chestOpen == true){
ChestOpened();
}
}
Thanks in advance.
I have the same problem.
I use this script
var hit : RaycastHit;
var matArray : $$anonymous$$aterial[];
private var index : int;
function Update () {
if (Input.Get$$anonymous$$ouseButtonDown(0) && collider.Raycast( Camera.main.ScreenPointToRay(Input.mousePosition), hit, $$anonymous$$athf.Infinity)) {
index++;
index = index % matArray.Length;
renderer.material = matArray[index];
}
}
please update this function so that objects overlapping each other does not trigger at once..
@Avengerxp, you should post this as a new question! Posting as an answer makes no sense and produces little or no result at all. Post a new question and explain more clearly what you expect to do and what it's doing wrong.
Answer by Democre · Nov 26, 2012 at 04:33 AM
You are using collider.Raycast, which in the docs says specifically that it "Casts a Ray that ignores all Colliders except this one."
What you would need to do is in some other Update function in a master script somewhere (so that it is only running once), use Physics.Raycast. In all the other objects just leave the OnMouseDown function that you have already, and remove the raycasts from their Update functions. This avoids the Raycasting from multiple objects each ignoring all other objects, and gives you one ray cast that will stop at the first object hit.
//in some game controller script
function Update () {
if (Input.GetMouseButtonDown(0))
{
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100.0))
{
hit.collider.gameObject.SendMessage ("OnMouseDown", null, SendMessageOptions.DontRequireReceiver );
}
}
}
Cheers!
Thanks for the advice, ill look into that tomorrow and see how I get on :)
Can thank you enough for a great answer and simple to implement.
Your answer
Follow this Question
Related Questions
Android touch 3d Object event 1 Answer
When I touch one object all others react 0 Answers
Ignore Touch 2 Answers
i need help in walking character in android, using raycast where user touches? 0 Answers