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 madmike6537 · Dec 01, 2012 at 10:27 PM · raycastscreenpointtoray

Need help understanding ScreenPointToRay

Hello friends and coders,

I am trying to instantiate a cube on the ground where I click with my mouse, but running into troubles. Its a third person game, so I am trying to raycast from my mouse click instead of from the head of my character like in a first person game.

I have been reading up on ScreenPointToRay, and I think it does what I want, but not sure.

My question is - does using ScreenPointToRay send a raycast from my camera towards the point where I clicked in the scene?

If so - any reason why this code wouldnt work?

 if(Input.GetButtonDown("Place Block") && placeBlock == true)                        //this will need changed
         {
 
             vRay = Camera.main.ScreenPointToRay(Input.mousePosition);
             
             if(Physics.Raycast(vRay, out hit,/* range, */ constructionLayer))
             {
                 Debug.Log ("raycast has hit " + hit.transform.name);
                 addingToConstructionLayer = true;

Its obviously not the whole code but I am trying not to overload my question with unnecessary code if at all possible. By the way, I get no errors, but nothing happens and the debug statement doesnt play.

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

2 Replies

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

Answer by Owen-Reynolds · Dec 02, 2012 at 04:17 PM

The overload with `(Ray, RaycastHit, ...)` takes distance as the 3rd input. You've got the layer as input 3, so it's using the layer you want to hit as the distance to shoot.

You're got a commented out range in there, just plug in 9999 (or Mathf.infinity)?

Getting the layers set up correctly can be tricky. Try it without the layerMask (just leave out contructionLayer) to be sure it's doing what you think. When you can mouseOver a rock and it prints "hit rock," add the layer. To avoid a giant scrolling list, can replace the Debug.Log with `hitWord=hit.transform.name` where hitWord is a global.

Comment
Add comment · Show 1 · 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 madmike6537 · Dec 02, 2012 at 10:47 PM 0
Share

Thanks - this ultimately led to me getting it to work. I originally had the range set to 100 but maybe setting it to 9999 fixed it. Its working now, thanks!

avatar image
0

Answer by CostelloNicho · Dec 01, 2012 at 11:21 PM

This line interests me:

 if(Input.GetButtonDown("Place Block") ..

Unless you have changed your input scheme you should be using (input.GetButtonDown("Fire1")) which checks for mouseOne being clicked. (This may be trivial but I'm not sure how you set it up.) I'm guessing this is the problem because you have no luck with the debug.

If it is what I am assuming, you are trying to make sure it hits something with a tag of "Place Block", Then you would want to search tags after doing the Raycast to make sure the tag is correct:

 If (hit.collider.tag == "Place Block") ....
Comment
Add comment · Show 1 · 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 madmike6537 · Dec 01, 2012 at 11:30 PM 0
Share

Thanks for the reply. That input is actually set to mouse 1, or right click via the input manager. I could remove it for simplicity sake for testing. But basically what is supposed to happen is when I right click the raycast is sent and if it hits the box is instantiated there.

I don't have any errors, but since nothing is happening I assume it's because my raycast isn't hitting anything.

I think more than anything I really just need to understand what screenpointtoray does so I can understand why it doesn't hit anything.

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

12 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

Related Questions

Lerp isn't completing itself 3 Answers

ScreenPointtoRay Problem, how does it work!?! 1 Answer

Ray not hitting certain colliders, can't figure it out, help? 1 Answer

ScreenPointToRay gives unexpected results? 2 Answers

My Player is crossing the ground through air 1 Answer


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