• 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
1
Question by jacksonkr · Jan 25, 2013 at 07:52 PM · raycasthitpoint

hit.point for rayhit not giving me what I'm looking for

I have a camara up high (y=100) looking straight down toward a ground (100x100x5 cube). I'm drawing a visble ray to where I click on the box and the ray shows up just fine (image)

alt text

but no matter where I click I get

 (0.0, -5.6, 0.0)
 UnityEngine.Debug:Log(Object)
 Level:Update() (at Assets/Level.js:26)

Which blows my mind. Here's the code:

 #pragma strict
 
 var ballPrefab:Transform;
 private var ball:Transform;
 private var lastMousePos:Vector3;
 
 function Start () {
     ball = Instantiate(ballPrefab, 
                         transform.Find("ballStartSpawn").transform.position, 
                         Quaternion.identity);
 }
 
 function Update () {
     var ray:Ray;
     if(lastMousePos != null) {
         ray = Camera.main.ScreenPointToRay(lastMousePos);
         Debug.DrawRay(ray.origin, ray.direction * 130, Color.red);
     }
 
     if(Input.GetMouseButtonDown(0)){
         lastMousePos = Input.mousePosition;
         var hit:RaycastHit;
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         
         if(Physics.Raycast(ray.origin, -Vector3.up, hit)) {
             Debug.Log(hit.point);
         }
     }
 }

I just want to find out where my ray from the camera is touching the pink cubes collider. Should that be hit.point?

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 sdgd · Jan 25, 2013 at 07:56 PM 0
Share

yeah I think hit.point is where the ray is colliding with collider

don't complettely understand ray for now but someone sometime gave me this code

and yeah this is C# not java

  if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$ouse1)){
 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 RaycastHit hit; // declare the RaycastHit variable
 if (Physics.Raycast(ray, out hit)) {
 Instantiate(TestObject, hit.point, transform.rotation);}
 }


ray.origin is where ray starts

2 Replies

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

Answer by sdgd · Jan 25, 2013 at 08:48 PM

 if(Input.GetMouseButtonDown(0)){
 lastMousePos = Input.mousePosition;
 var hit:RaycastHit;
 ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  
 if(Physics.Raycast(ray, out hit)) {
 Debug.Log(hit.point);
 }
 }

does this work?

I've coded this on the fly and don't know much of JS

but I've been trying to compare my provided C# code and yours

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 jacksonkr · Jan 25, 2013 at 08:53 PM 0
Share

Hah, that was totally it. All I changed was my line 25 to look like yours. So small! $$anonymous$$any thanks for your help buddy. I'd +1 if I could. Not enough rep yet :/

avatar image sdgd · Jan 25, 2013 at 09:18 PM 0
Share

yeah I learned how to from Aldo Naletto

http://answers.unity3d.com/questions/382686/ray-destination-x-y-z.html

that code you saw is from him for me

and it's kinda is strange because of:

 (Physics.Raycast(ray, out hit))

out hit - witch gives to me no idea why it works

avatar image
1

Answer by Wolfram · Jan 25, 2013 at 08:51 PM

Well, for the debug line you correctly compute the direction as ray.direction * 130, but for the raycast you use -Vector3.up, which is just straight down. The ray.origin is always (mostly) the same - a position very close to your camera (=on near clip plane), so just use ray.direction as direction, or as @sdgd suggested, just use the ray directly.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Raycasthit.point - same place every time! (Unity 3.5) 1 Answer

RayCast Hit Point rounding itself...? 1 Answer

How to get a Vector3 from a RaycastHit variable? 1 Answer

raycastHit.point - How do I make it ignore colliders? 1 Answer

Character keeps walking to origin with 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