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
1
Question by madmike6537 · Mar 19, 2013 at 01:29 AM · vector3distancemouseposition

How can I get the distance between two points, but on just one axis?

Ultimately I am trying to get the distance of the mouse from an object, but in a single axis, the y or x axis.

So I cant use Vector3.distance.

I thought about simply getting the mouse position in world points: Camera.main.ScreentoWorldPoint(Input.MousePosition) and then getting the y axis from that, and then simply subtracting from my other gameObject - but with the negative numbers I dont think that would work.

Then I looked into Mathf.Abs and I dont think that will help me either, but not sure.

Any thoughts on how I could do this? Thanks.

Comment
Add comment · Show 2
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 Chronos-L · Mar 19, 2013 at 01:36 AM 1
Share

Why $$anonymous$$athf.Abs is not helping?

Since

 straight-line distance = difference in point on straight line

Then

 distance = $$anonymous$$athf.Abs( x1 - x2 ) or 
 distance = $$anonymous$$athf.Abs( y1 - y2 )
avatar image madmike6537 · Mar 19, 2013 at 01:39 AM 0
Share

Hmm what if the position.y is negative lets say negative 3, then mathf.abs changes that to positive 3 - will the math still be right? If my position of my object.y is 12, and my position of my mouse.y is -3 - is their distance from eachother on the y access still 9?

3 Replies

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

Answer by hoy_smallfry · Mar 19, 2013 at 01:42 AM

Vector3.distance just uses a modified Pathagorean Theorem to get this equation. If you break that down to just one axis, that's:

 /* make sure both points are in the same coordinate system. 
  * That is, if you are using the mouse point, either
  * convert it to world coordinates or convert your object
  * to screen coordinates */
 Vector difference = pointA - pointB;

 var distanceInX = Mathf.Abs(difference.x);
 var distanceInY = Mathf.Abs(difference.y);
 var distanceInZ = Mathf.Abs(difference.z);


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 madmike6537 · Mar 19, 2013 at 01:49 AM 0
Share

Thanks - this ended up working like I needed. Thank you!

avatar image hoy_smallfry · Mar 19, 2013 at 01:51 AM 1
Share

Edit: If you are worried about actual direction, keep this in $$anonymous$$d always: vector substraction of A and B gives you a vector that essentially points from one vector to another. that relationship goes like this:

direction = to - from

avatar image
4

Answer by nuverian · Mar 19, 2013 at 01:45 AM

I think you can just do a Vector3.distance check, but having the axis of the target you dont want to check be same as the source like so:

 var from:Vector3 = new Vector3(0,0,0);
 var to:Vector3 = new Vector3(100,350,999);
 
 Vector3.Distance(from, Vector3(to.x,from.y,from.z));

(for x only in the example)

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 madmike6537 · Mar 19, 2013 at 01:49 AM 0
Share

Thanks very much for co$$anonymous$$g to answer!

avatar image nuverian · Mar 19, 2013 at 01:50 AM 0
Share

You are welcome :-)

avatar image
2

Answer by Neil Smith · Mar 21, 2014 at 11:12 AM

 var firstPoint;
 var secondPoint;
     
 function Update(){
     if(Input.GetMouseButtonDown(0)){
         var hit: RaycastHit;
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if(Physics.Raycast(ray, hit)){
             if(firstPoint==null){
                 Debug.Log('First Hit point: ' + hit.point);
                 firstPoint = hit.point;
             }else{
                 Debug.Log('First Hit point: ' + firstPoint + ' + Second Hit point: ' + hit.point);
                 secondPoint = hit.point;
                 var dist = Vector3.Distance(firstPoint,secondPoint);
                 Debug.Log('Distance: ' + dist);
             }
         }
     }
 }
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 rajavamsidhar_gvs · Jun 01, 2015 at 04:46 AM 0
Share

it works fine.thanks ....

avatar image vahid62 · Nov 21, 2015 at 08:29 AM 0
Share

Thanks. It works great for calculating distance

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

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

Related Questions

Getting vectors on an object every so many units 0 Answers

Shooting at mousePosition 1 Answer

Input.mousePosition to ScreenToWorldPoint 1 Answer

How do I find the farthest verts of a mesh relative to its object position and store them in an array 1 Answer

Distance between a gameObject and a direction vector. 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