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 interwound · Mar 08, 2012 at 04:30 AM · c#raycast

How to Raycast from Camera View Point?

Hey all, I looked at a bunch of questions but nothing seemed to answer my question. How do I Raycast towards something I'm looking at? Also, how would I place a cube at the place where the raycast hits? My code right now is:

  RaycastHit hit;
     void Update () {
             if (Input.GetButtonDown("Fire1")){
               bool doesCollide = Physics.Raycast(transform.position, transform.forward *10, out hit, 10);
               if(doesCollide){
                 GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                 cube.transform.position = hit.transform.position + hit.normal;
               }
             }
     }

But this tends to give weird results with cubes being places behind or in weird places. Also this script is attached to an object which is a attached my main camera (I don't know if that is the right terminology). Thanks in advance 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

2 Replies

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

Answer by Bunny83 · Mar 09, 2012 at 12:58 AM

@rutter is right, at least almost ;)

With hit.transform you access the object you've hit. .position will just return the pivot point of that object. That's not what you need. Take a look at the RaycastHit struct. It has a member called .point which hold the actual hit position in world space coordinates.

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

Answer by rutter · Mar 09, 2012 at 12:39 AM

On this line:

    cube.transform.position = hit.transform.position + hit.normal;

I'm not sure why you're adding the collision normal? More importantly, the base position of the object that was hit isn't necessarily quite the same as the point of impact. You probably want to use hit.point instead.

You asked about raycasting from the camera. If you want to cast a ray through a specific point on the screen, you could use Camera.ScreenPointToRay(). I haven't tried casting a ray through a specific object in the scene, but looking at this I imagine you might be able to use something like so:

    var origin = Camera.main.transform.position;
    var ray = new Ray( origin, transform.position - origin );
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 Bunny83 · Mar 09, 2012 at 01:00 AM 0
Share

He's adding the hit normal to move the cube a bit out of the surface he has hit. The pivot of the cube is in the center so if you place the cube at the hit point, the cube will be stuck half way in.

avatar image rutter · Mar 09, 2012 at 01:08 AM 0
Share

Ah, that does make sense. Thanks!

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Bottom area of a 3D model? Raycast? 1 Answer

How to get the distance between two objects in feet/meter? 1 Answer

Raycast with tags problem, resultless raycast.. 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