- Home /
Expert question - I want to find out the vector of the lowest vertex from the centre point of an object
Any Object. Mark it in the centre. Bee Line arrow down. Hit that final vertex. Get the vector point.
Then I want to raycast down from that vector.
So this would be the edge of an object, down from the centre to the bottom lowest y of the object.
Why am I do I need this. Ok colliders arnt working for me to achieve the physics I want. I need a centre point like this to detect things.
Answer by Xarbrough · Mar 03, 2018 at 02:03 AM
Here is some code to help you out:
using UnityEngine;
public class MyScript : MonoBehaviour
{
public MeshFilter meshFilter;
private Vector3 lowestVertex;
private Vector3 center;
private void Start()
{
if(meshFilter == null || meshFilter.sharedMesh == null)
return; // TODO: Error feedback.
lowestVertex = FindLowestVertex();
center = FindCenter();
}
private Vector3 FindLowestVertex()
{
float lowestYCoordinate = float.PositiveInfinity;
var lowestVertex = meshFilter.transform.position; // Some sensible default.
var vertices = meshFilter.sharedMesh.vertices;
for (int i = 0; i < vertices.Length; i++)
{
// Check for the lowest position in world space.
Vector3 worldCoord = meshFilter.transform.TransformPoint(vertices[i]);
if(worldCoord.y < lowestYCoordinate)
{
lowestYCoordinate = worldCoord.y;
lowestVertex = meshFilter.transform.InverseTransformPoint(worldCoord);
}
}
return lowestVertex; // In local space.
}
private Vector3 FindCenter()
{
Vector3 center = meshFilter.transform.position;
Vector3 sum = Vector3.zero;
var vertices = meshFilter.sharedMesh.vertices;
for (int i = 0; i < vertices.Length; i++)
{
sum += vertices[i];
}
center = sum / vertices.Length;
return center;
}
private void FixedUpdate()
{
Vector3 centerWorld = meshFilter.transform.TransformPoint(center);
Debug.DrawLine(
centerWorld + new Vector3(0, -0.1f, 0),
centerWorld + new Vector3(0, +0.1f, 0),
Color.red);
Debug.DrawLine(
centerWorld + new Vector3(-0.1f, 0, 0),
centerWorld + new Vector3(+0.1f, 0, 0),
Color.red);
Vector3 origin = meshFilter.transform.TransformPoint(lowestVertex);
Debug.DrawRay(origin, Vector3.down, Color.green);
// TODO: Raycast
}
}
"Any object" is a little too broad, so I assumed you were talking about meshes. If you don't use the transform as a pivot, you can take the average of all vertices to find a center position.
To find the lowest vertex position in a mesh you have to loop through all the vertices and compare each one.
Yes, I knew it involved the mesh, but referred in the question as context of the object. Very happy with this response. Thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220612153827im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Car Movment Script not fully working!!!!!!! 1 Answer
(at very high altitude many objects are created)How to make the object broke from falling answer 1 Answer
Performing if loop multiple times to perform attack animation 1 Answer
Basics on 2d gameplay for a 2.5d Game 1 Answer
Pysics not working as expected 1 Answer