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 Trithilon · Sep 05, 2012 at 08:35 PM · vector3mathprojectraypoint

Math Question : How to Get a Point on a Ray which is EXACTLY 5 units away from Vector3.Zero?

As the title suggests I need to find a Point on a Ray which is at a certain distance from another specified point (Vector3.zero in this case).

More info : I am trying to make a Draggable Sphere around a planet which ALWAYS maintains a 5 unit distance from the Center (Vector3.zero) and cannot be dragged beyond that. Dragging it closer to the Planet will only make it hover and travel in the direction of the drag.

EDIT:

Here is what I have right now... http://dl.dropbox.com/u/9030688/RadialDragger/RadialDragger.html

How do I make the Small sphere align to the surface of a virtual sphere which is 5 units in radius? (I dont wish to use ray casts on sphere collider)

And here is my Code :

http://pastebin.com/PaV4Rr7B

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

5 Replies

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

Answer by Eric5h5 · Sep 05, 2012 at 10:22 PM

Use Ray.GetPoint.

Comment
Add comment · Show 5 · 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 Trithilon · Sep 06, 2012 at 07:08 AM 0
Share

Yes.... but GetPoint only gives me a point from the Ray. But It needs a distance. I don't know at what distance will I make sure that the Point it GET will be exactly 5.0f from Vector3.zero.

avatar image Eric5h5 · Sep 06, 2012 at 01:36 PM 1
Share

GetPoint takes a float, which is the distance. So if you use 5.0, assu$$anonymous$$g the origin of the ray is Vector3.zero, then the distance from there is 5.0.

avatar image Trithilon · Sep 06, 2012 at 01:46 PM 0
Share

But my Ray starts from the Camera. And I need to get the point in the world on the ray which would essentially be 5 units from (0,0,0). That is the issue...

avatar image Cripple · Sep 06, 2012 at 04:18 PM 2
Share

Vector3 point = centerOfSphere + ray.direction -1 5.0f;

You can multiply a direction by -1 to get its opposite.

avatar image Trithilon · Sep 07, 2012 at 11:10 AM 0
Share

Hey, Thanks it works now. :) Just that I need it to be exactly under my finger now. Will try to figure that out. :)

avatar image
2

Answer by Cripple · Sep 05, 2012 at 09:05 PM

Hi, you should read some doc about vectors & stuff.

http://docs.unity3d.com/Documentation/Manual/VectorCookbook.html

Here is what you are looking for :

Vector3 point = centerOfSphere + ray.direction * 5.0f;

You add to the origin point the vector which will give you your final point. You want to follow the ray so you get its direction (it is normalized, so its magnitude is 1) and you multiply it by the distance you want to travel (here 5 units).

Comment
Add comment · Show 1 · 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 Trithilon · Sep 06, 2012 at 06:58 AM 0
Share

Hello, I tried but it wont work. It essentially kind of ends up projecting it on a plane. I wish to - you could say - project it on the surface of a virtual sphere. You should be able to see half of what I am trying to achieve in the webplayer demo I posted. It never comes closer or further away from the sphere. I want it to Vertically snap and travel along the surfacenormal (without using raycasts or invisible spehere colliders) when I try to drag it closer to the Planet.

avatar image
1

Answer by FWCorey · Nov 30, 2012 at 06:42 AM

Make the sphere a child object of a child object of the planet at exactly 5 units away along the z axis and just put a LookAt script on the sphere's parent to look at a dummy object that is always synced to the mouse pointer. That should give you the effect you are looking for with the least effort.

Comment
Add comment · Show 1 · 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 Trithilon · Nov 30, 2012 at 09:48 AM 0
Share

Naah... I want to avoid such work arounds and keep everything controlled via script. In anycase... answers I got were good enough for me. :)

avatar image
0

Answer by Piflik · Sep 05, 2012 at 09:09 PM

You need the point on the ray, that is closest to your origin. Measure the distance and then you can calculate the two points (it's always two) on the ray that are 5 units from the origin (you get the distance between the closest point and your wanted point by way of the theorem of Pythagoras). But I am not sure this will help you at all...

What I would do, is calculate the distance between the sphere and the planet, and if it is bigger or smaller than a certain threshold, you set it to be that exact threshold.

Example: var maxDist : int; var minDist = int; private var direction : Vector3;

 function Update() {
     direction = sphere.position - planet.position;
 
     if(direction.magnitude > maxDist)
         sphere.position = planet.position + direction.normalized * maxDist;
     if(direction.magnitude < minDist)
         sphere.position = planet.position +direction.normalized * minDist;
 }
Comment
Add comment · Show 1 · 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 Trithilon · Sep 06, 2012 at 07:02 AM 0
Share

If I got you correctly, I have already achieved this. But its on a 2D plane and always keeps the distance snapped to 5.0f. I want to snap it along the surface of the planet so that it will start hovering above the sphere when I try to pull it into the sphere. (As if it was linked to the center of the sphere with a fixed joint that maintains a distance of 5 units)

avatar image
0

Answer by Foam · Sep 05, 2012 at 10:41 PM

You should note that EXACTLY 5 units is a tricky thing when using floats and math operations.

If you're using LARGER THAN that's polled frequently enough you should be fine but just using 1 check can be fatal due to rounding errors.

Comment
Add comment · Show 1 · 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 Trithilon · Sep 06, 2012 at 07:10 AM 0
Share

You want me to run a for loop using ray.Getpoint(forloopcounter) ? And approximate which one was the closest? I will be limited with the resolution I can get. Check my post - I have add a webplayer build to show you what I have and described what i want.

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

12 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

Related Questions

Project a position onto a plane whose normal isn't at the origin. 2 Answers

Closest point on multiple lines? 2 Answers

Average of Vectors [Math] 1 Answer

How to get a collider from point? 1 Answer

Rotate 3d vector based on local forward 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