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 /
  • Help Room /
avatar image
0
Question by ShroomWasTaken · Oct 11, 2015 at 05:12 PM · c#instantiateraycasting

Raycast never gets called

So, I made a script where something should be instantiated where the mouse is positioned. However if (Physics.Raycast(ray)) {} never gets called. I can't seem to figure out why... Here's my current code : (this is nto the full code, but I think this is the only part needed, if not tell me. :) )

                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
                 if (Physics.Raycast(ray))
                 {
 
                     if (Input.GetButtonDown("Fire1"))
                     {
                         Instantiate(towers[i], Input.mousePosition, transform.rotation);
                         Debug.Log("Instantiated");
                         towerRayBools[i] = false;
                     }
 
                 }
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
0

Answer by MadDevil · Oct 12, 2015 at 04:47 AM

@nobx101

you have written the if conditions in a wrong way, you are raycasting before pressing the button.

try this,

 if(Input.GetMouseButtonDown(0))
         {
             Ray ry = Camera.main.ScreenPointToRay(Input.mousePosition);
 
             if(Physics.Raycast(ry, out hitinfo, Mathf.Infinity))
             {
                 Instantiate(towers[i], Input.mousePosition, transform.rotation);
                 Debug.Log("Instantiated");
                 towerRayBools[i] = false;
             }
 
         }



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 ShroomWasTaken · Oct 12, 2015 at 05:00 AM 0
Share

@$$anonymous$$adDevil That didn't work :/ It's not really what I want either, I want the raycast to be "displayed" even though the player didn't press mouse button 0. I just want it to instantiate there when the button is pressed.

avatar image MadDevil ShroomWasTaken · Oct 12, 2015 at 05:11 AM 0
Share

@nobx101

if you just want to instantiate anything by pressing a button, why are you using the raycast ??

and if you just want to debug a ray then use Debug.Drawray, rather than raycasting with help of physics.

avatar image ShroomWasTaken MadDevil · Oct 12, 2015 at 05:58 AM 0
Share

@$$anonymous$$adDevil I use the raycast just to check if there's already a placed tower where the player wants to place a new one.

Show more comments
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

32 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

Related Questions

How do you prevent instantiation of objects inside each other with proper orientation? 0 Answers

Raycast doesn't stay in one place 0 Answers

Can You help me Fix This? (Infinite level Generator) 0 Answers

Animating a click to move character 1 Answer

How can I change this script so that each instantiated prefab spawns a set amount faster 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