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 TPitloun · Nov 13, 2012 at 05:03 PM · raycastcolliderfps

How to use raycast when standing still.

I made a script that shoots a raycast through the Mouse point to hit a collider. The problem is that it only will contact the collider if my player is moving. function Start () { }

 function Update () {
     Screen.lockCursor = true;
     if(Input.GetKey("l")){
         Screen.lockCursor = false;
     }
     if(Input.GetMouseButtonDown(0)){
         //Debug.Log("LEFT SWAGGER LOL");
         
         var hit : RaycastHit;
         var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         
         if(Physics.Raycast(ray,hit,20.0)){
             //print("ERMEHGERD YOU HIT SOMETHING");
             if(hit.collider.tag == "Tree"){
                     Debug.Log("Tree Hit");
                     hit.collider.transform.GetComponent(scriptTree).hitCount();
                 }
                 else
                 {
                     Debug.Log("Nothing");
                 }
             }
         }
 
 }

I think it has something to do with the Screen.lockCursor, but Im not sure. Thank you for your help!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by AlucardJay · Nov 13, 2012 at 05:52 PM

If you are using Screen.lockCursor while playing in the Unity Editor, left mouse click breaks this. Then there are all sorts of problems with Input.mousePosition after that. If you are using Screen.lockCursor, then it would be safe to assume you always expect the mouse to be in the center of the screen. Try this :

 var ray : Ray = Camera.main.ScreenPointToRay( Vector3( Screen.width * 0.5, Screen.height * 0.5, 0.0 ) );

Your ray will then always be cast from the center of the screen, even when Screen.lockCursor breaks in the editor (and webplayer!).

Comment
Add comment · Show 8 · 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 TPitloun · Dec 05, 2012 at 02:37 PM 0
Share

Sorry for really late reply but it still doesn't work for me if my character is standing still. Could this be because I'm using the first person controller.

avatar image AlucardJay · Dec 06, 2012 at 03:09 AM 0
Share

In that case, start testing bit-by-bit. What happens when you use :

 if ( Input.Get$$anonymous$$ouseButtonDown(0) )
 {
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     
     if(Physics.Raycast(ray,hit,20.0))
     {
         Debug.Log( "$$anonymous$$ouseRay Hit : " + hit.collider.gameObject.tag );
         
         if ( hit.collider.gameObject.tag == "Tree" )
         {
             Debug.Log("Tree Hit");
             var treeScript : scriptTree = hit.collider.transform.GetComponent( scriptTree );
             treeScript.hitCount();
         }
         else
         {
             Debug.Log("Nothing");
         }
     }
 }


BTW you should really have your classes starting with a capitol, and your variables as camelCase :

 var scriptTree : ScriptTree;
avatar image TPitloun · Dec 06, 2012 at 02:25 PM 0
Share

Right so it still has the same problem it shoots the Raycast but wont notice the tree until I am moving. Standing still and looking at tree ($$anonymous$$ouse Ray Hit: Untagged) $$anonymous$$oving and looking at the tree ($$anonymous$$ouse Ray Hit: Tree).

Gonna make a few debug objects in the scene and see if its just the mesh maybe.

avatar image AlucardJay · Dec 06, 2012 at 11:16 PM 0
Share

If your tree has a collider, try using primitive colliders like a capsule collider. If using mesh colliders, make sure the import settings are set to convex. There is also a problem with mesh colliders culling the back (unseen) side, so again try with just a primitive collider

avatar image TPitloun · Dec 08, 2012 at 10:25 PM 0
Share

I have tried this before and had the same issue. I tryed using a capsule and a box collider.

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

10 People are following this question.

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

Related Questions

Hit distance Change light range 1 Answer

Instantiated bullet hole has wrong rotation 1 Answer

ball sometimes goes through the bat when hit... collider problem... How to do a Raycast? 0 Answers

Raycast isnt working 1 Answer

Raycast on a 2D Collider 7 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