- Home /
How can I find the Distance between Two Objects on the X Axis?
Hi, I am trying to find a way to find the distance between to points ONLY ON THE X AXIS. By this I mean say my transform.position.x was 5 and my player.transform.position.x was 10, I would receive a result of five. If anybody knows how to do this it would be much appreciated if you could let me know how.
Many Thanks,
Tommy Eaves
Answer by $$anonymous$$ · Aug 07, 2015 at 08:16 PM
float dist = object1.transform.position.x - object2.transform.position.x;
Answer by Djaydino · Aug 02, 2021 at 02:42 PM
Hi. you actually need to use Mathf.Abs, this way it always gives a positive value:
float dist = Mathf.Abs(object1.transform.position.x - object2.transform.position.x);
Answer by vogles · Aug 07, 2015 at 07:19 PM
Distance from one point to another : point2 - point1. Then just get the x property
Your answer
Follow this Question
Related Questions
code for detecting if a 2d infinite runner player stopped moving because it hit a wall or something 1 Answer
How is the Bounds.SqrDistance calculated behind the scene? What algorithm/formula does it use? 2 Answers
ARCore tracking maximum distance 2 Answers
Limit the distance between a GameObject and a Character Controller 0 Answers
Calculate the distance between an object and my player 1 Answer