- Home /
Point on mesh at certain distance
Hey guys, I have a patrolling AI state, I want to be able to pick a certain point on that mesh at random, but that point cant be further than a certain - Radius (Patrol radius) I know how to get the mesh i'm on, I know how to do this on a terrain, but no idea how to do it on a random mesh that I am on, any help?
Answer by Artaani · Dec 13, 2016 at 07:31 AM
If I understood you right, you need to use function Collider.OnTriggerEnter
Create some class, for example "TargetFinder", attach it to your AI object, also attach Sphere Collider componentwith desired radius and use the next code:
using UnityEngine;
using System.Collections;
public class TargetFinder : MonoBehaviour {
void OnTriggerEnter (Collider other) {
// other - this is any other colliders which enters your trigger
// let's take its mesh
Mesh otherMesh = other.GetComponent <MeshFilter> ().mesh;
// But make note, this function will find everything, including AI, terrain etc.
// So you need to filter it somehow. For example using tag
if (other.gameObject.tag == "TrackableUnit") {
Mesh otherMesh = other.GetComponent <MeshFilter> ().mesh;
}
}
}
I want a certain point/ vertex on the mesh itself, so assu$$anonymous$$g that the mesh is a floor then I will find a random point at a maximum radius distance on this mesh, not the mesh itself, a vertex on that mesh is at a distance of maximum radius from AI unit