- Home /
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!
Btw it's two words there "out hit" so that might error.
@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.
Answer by Berenger · May 11, 2012 at 03:25 PM
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.
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.
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.
try :
if(Physics.Raycast(transform.position , transform.forward, out hit, maxDistance)){
your Vector3 is a world space reference in this case
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.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Trying to make a laser beam ability using raycast 1 Answer
GameObject once walked on, gives message using C#? 1 Answer
how do i fix this small error 2 Answers