Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Gilead7 · May 11, 2012 at 03:18 PM · c#raycastcoroutine

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!

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image maroonrs2 · May 11, 2012 at 03:21 PM 0
Share

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

avatar image maroonrs2 · May 11, 2012 at 03:21 PM 0
Share

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

avatar image Gilead7 · May 11, 2012 at 04:02 PM 0
Share

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

avatar image AlucardJay · May 11, 2012 at 04:10 PM 2
Share

@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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image maroonrs2 · May 11, 2012 at 03:26 PM 0
Share

This should belong kn a comment.

avatar image Berenger · May 11, 2012 at 04:09 PM 2
Share

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.

avatar image Gilead7 · May 11, 2012 at 04:19 PM 0
Share

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.

avatar image AlucardJay · May 11, 2012 at 04:22 PM 2
Share

try :

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

your Vector3 is a world space reference in this case

avatar image Berenger · May 11, 2012 at 04:22 PM 2
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges