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 citiral · Jul 14, 2011 at 11:27 AM · javascriptraycasthitcreateexplain

Raycasthit explanation wanted.

I'm trying to make a simple script that creates a cube on the place I click on a Plane. I figured I had to do that with RaycastHit, but there are no proper examples of what I want to achieve. Can someone please properly explain raycasthits and the likes, and also on which object I have to put the script on, eg: does the raycasthit only detects hits on the object the script is attached to?

Thanks in advance.

Comment
Add comment · Show 1
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 citiral · Jul 14, 2011 at 02:13 PM 0
Share

Huh, what? Offcourse not, I was trying to make a basic RTS test, but I had to know how raycasts worked, so I figured the easiest way to do so is to make a box spawn on the location of where I click on a plane. Nothing $$anonymous$$ecrafty about that.

2 Replies

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

Answer by Spk · Jul 14, 2011 at 02:27 PM

A raycast hit is not associated with any particular object. Essentially, you provide a starting position, direction and optionally a hitmask (to filter out what you want/not want to hit), and you eventually get back a RaycastHit record containing the various information you might need, including which GameObject has been hit, along with the intersection point and normal.

In your case, you just need to create a new script, called whatever, where you will put your input & raycast logic.

In Update() you will monitor the click event from the user. When this happens, you should call a function with the following pseudo/sample code:

 // create a ray going into the scene from the screen location the user clicked at
 Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
 
 // the raycast hit info will be filled by the Physics.Raycast() call further
 RaycastHit hit;
 
 // perform a raycast using our new ray. 
 // If the ray collides with something solid in the scene, the "hit" structure will
 // be filled with collision information
 if( Physics.Raycast( ray, out hit ) )
 {
         // a collision occured. Check if it's our plane object and create our cube at the
         // collision point, facing toward the collision normal
     if( hit.collider == yourPlaneCollider )
         Instantiate( yourCubePrefab, hit.point, Quaternion.LookRotation( hit.normal ) ); 
 }

Hope this helps.

Comment
Add comment · Show 2 · 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 citiral · Jul 14, 2011 at 02:33 PM 0
Share

Thanks, that really helped a lot.

avatar image vas11 · Dec 28, 2015 at 03:56 PM 0
Share

That's really helpful! For clarification, could you please explain the syntax out hit? Does this somehow initialize hit, allowing Physics.Raycast to deter$$anonymous$$e where the collisions are along the ray? Also, what if there are multiple collisions along the ray?

avatar image
2

Answer by Skjalg · Jul 14, 2011 at 02:09 PM

Isn't your question answered here ? http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html?from=RaycastHit

hitInfo will contain more information about where the collider was hit (See Also: RaycastHit).

 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 var hit : RaycastHit;
 if (Physics.Raycast (ray, hit, 100)) {
     Debug.DrawLine (ray.origin, hit.point);
 }casting a ray.
Comment
Add comment · 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

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

Spawn a dot for every number typed? 0 Answers

How can I make an object move towards the camera when it's being looked at and go back to its original position when it's not longer being looked at? 0 Answers

[fixed] My physics.raycast vision doesn't work as I expect it to, and I don't know why 2 Answers

Js autocomplete not working in 5.3.4 f1 2 Answers

Cant switch between legacy animations 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