x


Raycasting hitting sometimes, not hitting sometimes

I am getting some weird behaviour regarding raycasting. I have used this exact method of raycasting in two other places where it continues to have no problems. When the ray does hit, all the code executes fine and behaves as it should. I often will click the same thing more than once and eventually it will work. The problem is alot worse when I build and run the project. I have like a 50% chance of it working in the editor, and about a 5% chance working in game. It prints 'Something wrong with raycast' when it does fail.

function Update() {
    if(GameGui.pcubeModeOn == true && dragOn == true){
        if(Input.GetMouseButton(0) == false) {
            LineRender.lineOn = false;

            // cast a ray from camera to mouse position
            var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
            var hit : RaycastHit;
            var aCollider : Collider = collider;

            if (aCollider.Raycast (ray, hit, 10000.0)) 
            {
                var tVoxType : int = aCollider.GetComponent(CubeInfo).cubeVoxId;
                if(tVoxType == 18 && connectionAddAllowed == true) {
                    var LineRenderObj = GameObject.Find("Main Camera");
                    LineRenderObj.GetComponent(LineRender).AddConnection(LineRender.targetPowerCube , aCollider);
                    connectionAddAllowed = false;
                }
            } else {
                print("Something wrong with raycast");
            }

            dragOn = false;
        } 
    }

}

Any ideas what could be wrong?

more ▼

asked Feb 07 '11 at 05:36 PM

James 12 gravatar image

James 12
17 1 1 5

Could you go into more detail about the collider(s) you are expecting to hit? It's acting like the ray isn't hitting anything, and your code as written assumes that it will never miss.

Feb 07 '11 at 08:42 PM Bampf

The collider i am expecting to hit is a cube that I instantiate from a prefab (box collider). What i am doing is dragging the cursor from a start cube. A line is drawn from there to the pointer. Then if you drop the line on any other cube it saves the connection in an array and lines are drawn to connect them. This ray (with the problem) is the one to detect if you are over a cube when you let go of the cursor. I use a ray for the first cube and it works every time.

Feb 07 '11 at 09:00 PM James 12
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I'm not sure if this is what you are doing, but I noticed that my raycasts weren't working when I had two fingers on the screen. the problem was that I wasn't searching through all of the touches when checking if the raycast hit. I put my raycast inside of a foreach(touch) loop and solved the problem.

more ▼

answered Feb 07 '11 at 06:36 PM

chris 5 gravatar image

chris 5
57 6 6 10

This is inside of an update function, so it is being run every frame. I'll update the top with the full function.

Feb 07 '11 at 07:01 PM James 12
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1528
x657
x179

asked: Feb 07 '11 at 05:36 PM

Seen: 1088 times

Last Updated: Feb 07 '11 at 07:02 PM