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.
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*);*
-
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*;_
_ }_
_ 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.