Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Nov 29, 2016 at 03:51 PM by Bunny83 for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by Xenon42 · Nov 28, 2016 at 11:41 PM · scripting problemraycastingscreenpointtoray

ScreenPointToRay not responding

I'm trying to create a simple RTS, and currently I'm trying to nail down the basic functionality of pointing at a unit and selecting it. My intended solution involves raycasting, specifically ScreenPointToRay.

 void Update ()
     {
         print(Input.mousePosition);
 
         if (Input.GetMouseButtonDown(1))
         {
             RaycastHit hit;
             Ray ray = gameCamera.ScreenPointToRay(Input.mousePosition);
 
             if (Physics.Raycast(ray, out hit))
             {
                 print(hit.transform.position);
             }
         }
 
         if (Input.GetMouseButtonDown(2))
         {
             if (unitSelected == true)
             {
 
             }
         }
     }

I was trying to test my code by having the script return the world space position that it had hit, but for some reason it never returns a value. I tried the raycasting function without the if check, so it ran every Update, and that seemed to return some world space coordinates, but very infrequently.

I'm very confused as to what's going wrong here to cause such odd behaviour, and I can't find an answer anywhere else.

Comment
Add comment · Show 3
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 Bunny83 · Nov 29, 2016 at 12:48 AM 1
Share

Uhm you know that you're actually checking the right mouse button for the raycast and the middle mouse button in your second if statement? $$anonymous$$aybe that's already your problem? Indices start at "o":

 0 -> left mouse button
 1 -> right mouse button
 2 -> middle mouse button
avatar image Xenon42 Bunny83 · Nov 29, 2016 at 10:36 AM 0
Share

Oh. Now it works. Herp a derp I'm stupid.

I think I was confusing mouse button "1" with "Fire1". I'll have to remember they're not the same for next time,

avatar image Bunny83 Xenon42 · Nov 29, 2016 at 03:50 PM 0
Share

So we can close this one, right? ^^

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

75 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can you figure out raycast origin position from RacyastHit? 2 Answers

Lock rotation of player 2 Answers

Problem creating submeshes based on raycast. 0 Answers

raycast screenpointtoray isnt working for some reason 0 Answers

SphereCastAll - Getting data out of "Order By"+ "ToArray" 0 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