- Home /
Is there a way to calculate the distance between the origin and the object´s edge?
So I have an object that I want to positionate immediately after another.
public GameObject previousObject;
MeshFilter mf;
MeshFilter prevMf;
void Start()
{
mf = GetComponent<MeshFilter>();
prevMf = previousObject.GetComponent<MeshFilter>();
}
void Update ()
{
this.transform.position = previousObject.transform.position + new Vector3 (-prevMf.mesh.bounds.size.x / 2 - mf.mesh.bounds.size.x / 2, 0, 0);
}
However, for this to work I need the origin to be at half max sizes (which works with cubes, but with other figures it´s different), which may not always be the center. So one solution is to move the gameObject´s origin to that position, but it´s kinda tedious to do this with every object. So again, is there a way to calculate the distance between the origin and the object´s edge?
Answer by Feref2 · Oct 03, 2018 at 11:01 PM
Thanks to both for your answers, but I accidentally found my own solution. The answer is not to calculate the distance between the origin and the edge, but to move the origin to the center of the largests points. That way, that distance will always be size.x / 2. Do this with every object and also make sure the y axis are aligned.
Answer by tormentoarmagedoom · Oct 02, 2018 at 01:12 PM
Good day.
I think there is not. By the simple way, distance only can refear between 2 points, not 1 point and 1 plane or line... What you can do is create a empty object as child at the edge and mesure the distance between origin and that empty child.
Answer by Zodiarc · Oct 02, 2018 at 01:35 PM
You could get a list of the vertices from the MeshFilter, then you have the local position of each vertex. You can use Transform.TransformPoint() to calculate their world space, then use a Comparer (which internally uses Vector3.Distance()) to sort the list by distance and take the last element of the list. This should be your furthest edge.
Your answer
Follow this Question
Related Questions
Mecanim: move with mouse 1 Answer
Set World Camera Position in View Matrix 1 Answer
iPhone tracking distance from computer? 0 Answers
ARCore tracking maximum distance 2 Answers
Can I offset a mesh's transform? 1 Answer