x


Raycast not allowing coroutine

Been able to not open the chest from far away, but now I can't open the chest while standing in front of it.

public void OnMouseUp(){
    Debug.Log("Mouse Up");
    if(Physics.Raycast(transform.position , Vector3.forward, out hit, maxDistance)){
       if(hit.collider.gameObject.tag=="Chest"){

    switch(state){
       case ChestState.open:
       state=ChestState.inbetween;
       StartCoroutine("CloseChest");
       break;

       case ChestState.closed: 
         StartCoroutine("OpenChest");
       break;
       }
    }
    }
}

Max distance is 2f Tag Chest has been enabled. Where am I going wrong? Thanks!

more ▼

asked May 11 '12 at 03:18 PM

Gilead7 gravatar image

Gilead7
117 18 32 39

What is outhit? Have you tried adding 2 to it?

May 11 '12 at 03:21 PM maroonrs2

Btw it's two words there "out hit" so that might error.

May 11 '12 at 03:21 PM maroonrs2

yet, if I put a comma between them, I get an error.

May 11 '12 at 04:02 PM Gilead7

@maroonrs2 this is C# and how raycast is done ....

@Gilead , try more debugging to see where it is breaking. after the raycast if :

Debug.Log("RayHit Collider Tag = " + hit.collider.gameObject.tag);

after the tag if statement :

Debug.Log("RayHit Chest. state = " + state);

this should tell you what the ray is hitting, and if its the chest what the state is.

May 11 '12 at 04:10 PM alucardj
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Could be a lot of reasons.

Maybe your not using units as meters (meshes too big for instance) and masDistance is actually a very small distance.

Maybe the chest's tag is wrong, maybe it's collider is to small, or not high enough and the ray goes over the top. Maybe Open / CloseChest aren't correct. Maybe there is a third state.

more ▼

answered May 11 '12 at 03:25 PM

Berenger gravatar image

Berenger
11k 12 19 53

This should belong kn a comment.

May 11 '12 at 03:26 PM maroonrs2

How so ? I gave all the possibles error I could think of given the information the OP told us. That's the best answer in that situation, imo.

But if you disagree, please answer correctly to other questions and gather enough karma to move this one into comments. By the way, "out hit" is correct and means hit is passed as an unitialized reference.

May 11 '12 at 04:09 PM Berenger

I enabled a Debug.drawray and took a look. Even though I am facing forward, the line is shooting away from me. Somehow, I need to turn the ray around, unless I'm missing something else.

May 11 '12 at 04:19 PM Gilead7

try :

if(Physics.Raycast(transform.position , transform.forward, out hit, maxDistance)){

your Vector3 is a world space reference in this case

May 11 '12 at 04:22 PM alucardj

Ah ok. I didn't notice it, but you're using Vector3.forward in your raycast. It's a constant and its value is (0,0,1). You need to use transform.forward, which is that constant rotated by the gameobject's rotation.

May 11 '12 at 04:22 PM Berenger
(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:

x4138
x1525
x323

asked: May 11 '12 at 03:18 PM

Seen: 434 times

Last Updated: May 14 '12 at 03:40 PM