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 sdgd · Feb 28, 2013 at 02:09 PM · c#raycastfloatingpointhit.point

Raycasting how to find position on end of ray float distance

 Ray ray = gameObject.camera.ScreenPointToRay(Input.mousePosition);
 if (Physics.Raycast(ray, 5/*Float Distance*/)){
     transform.position = /*vector 3 ( position at end of ray distance )*/
 }

let say between me and float distance lies box if I would look at box I would get position

vector 3 (0,0,1)

I would achieve that with this code

 Ray ray = Player.camera.ScreenPointToRay(Input.mousePosition);
 RaycastHit hit; // declare the RaycastHit variable
 if (Physics.Raycast(ray,out hit)) {
     transform.position = hit.point
 }

hit point would be vector 3 (0,0,1)

but if that box is still there I want to find the vector 3 (0,0,5) so it would go through box and on float distance would stop and look it's position

I hope I explained well what I need

if not I'll need to make a picture

done on request Picture

for unity ask a question.jpg (33.5 kB)
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 Mikilo · Feb 28, 2013 at 02:12 PM 0
Share

$$anonymous$$ake a picture... XD I didn't understand after Vector3(0,0,1) and Vector3(0,0,5).

1 Reply

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

Answer by AlucardJay · Feb 28, 2013 at 02:16 PM

Check my answer here : http://answers.unity3d.com/questions/406636/3d-object-attach-to-mouse-1.html

If you don't have a collider to raycast against for depth reference, but you know the distance from the camera you would like the point to be, you can use Ray : http://docs.unity3d.com/Documentation/ScriptReference/Ray.html

 #pragma strict
 
 public var distance : float = 1.5;
 
 function Update() 
 {
     CastRayToWorld();
 }
 
 function CastRayToWorld() 
 {
     var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var point : Vector3 = ray.origin + (ray.direction * distance);

     Debug.Log( "World point " + point );
     Debug.DrawLine( Camera.main.transform.position, point, Color.red );
 }

Distance is distance from the camera that the ray is to terminate, play with this value. The Debug.DrawLine should show where this point is in world space.

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 sdgd · Feb 28, 2013 at 02:31 PM 0
Share

thanks

but as always I hate java had to convert it

 Ray ray = gameObject.camera.ScreenPointToRay(Input.mousePosition);
 PickUpObjectScript.x.transform.position = ray.origin + (ray.direction * 5);

I thank you very much still :)

avatar image AlucardJay · Feb 28, 2013 at 02:52 PM 1
Share

Sorry about that, as you can see I just quickly modified this from the other answer for you without taking notice of your tag. D'oh! But yeah, looks like you had no problems converting. The Unity script reference would have been helpful too for that I hope. Happy Coding =]

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

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

Strange Behavior with Hit.point 1 Answer

Multiple Cars not working 1 Answer

any else way finding floating point 1 Answer

Distribute terrain in zones 3 Answers

[C# / Unity] Raycast Ignoring Self / Offsetting Instantiations? 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