I’m building a simple predator eats prey model where predators search a box using a field of vision (mesh I made that acts like a cone from the object called predator view) and will act depending on what collides with it’s field of vision. I try to set oncollision with this mesh or an ontrigger with the mesh but even debus.logs i’m putting into the oncollision/ontriggers won’t display anything. Since the predator view is a child of the predator is this a problem and if so how can i pass something up from the child argument when there is a collision with the mesh. Also i have the mesh set as kinematic if that helps.
I hope this makes sense but ideally i’m looking for a way for whatever is colliding with the child mesh, that object gets passed up to the parent so the object can react accordingly, and if its a wall turn around or if its a prey chase after it.
code would probably help more and can post some if it will help but for now heres a pic
i’ve been messing with it and so far OnCollisionEnter in the predator object only populates when on the actual game object has a collision and not the child mesh object. I’ve tried giving the mesh rigidbodies and colliders but am unsure how to go about getting the collision detecting for it, and then passing it to the parent. using c# btw
i have done such a setup a couple of weeks ago, but i did a slightly different approach using raycasting.
But you are referring to onCollisionEnter have you tried removing the actual mesh from his “sight” and use your cone as a trigger instead. And use “OnTriggerEnter” when something enters your childs trigger area, it does something to its parent?
The object with the rigidbody is the object that receives the collision events.
you can have colliders on children that will (should from memory) trigger the events on the parent.
james yea thats what i thought too, maybe i have it set up weird but the sphere needs the rigidbody so it doesn’t go through the walls, the mesh (predetor view) i’ve tried removing the rigidbody on and off but still sets no collision warning itself. The only collisions i can produce is when the actual sphere hits something and not the vision cone.
Toerk i’m not quite sure i understand removing the mesh form his sight, but when i use the OnTrigger even on the child i would think it would do something but my debugs aren’t populating, checking on and off the isTrigger button in predator view
its almost like the colliders for the predator view aren’t even doing anything, i tried taking out the predetor view and letting it sit there on its own and still nothing triggers.
heres what my predetor view lookds like
and the mesh code if you want to search through it
using UnityEngine;
using System.Collections;
public class dmesh : MonoBehaviour {
private Collider myCol;
private Mesh myMesh;
public float radius;
public float angle;
public float segments = 2;
private float segmentAngle;
private Vector3[] verts;
private Vector3[] normals;
private int[] triangles;
private Vector2[] uvs;
private float actualAngle;
void Start(){
gameObject.renderer.material.color = Color.green;
//get the mesh on the gameobject
myMesh = gameObject.GetComponent<MeshFilter>().mesh;
myCol = gameObject.GetComponent<Collider>().collider;
//clear it first
myMesh.Clear();
// Calculate pangle
actualAngle = 90.0f - angle;
//Segment angle
segmentAngle = angle*2 / segments;
//initialize length of arrays
verts = new Vector3[(int)segments * 3];
normals = new Vector3[(int)segments*3];
triangles = new int[(int)segments*3];
uvs = new Vector2[(int)segments*3];
//initialize array to origin points
for (int i = 0; i < verts.Length;i++){
verts[i]= new Vector3(0,0,0);
normals[i] = new Vector3(0,0,0);
}
//tempangle
float a = actualAngle;
//create verts
for (int i = 1; i < verts.Length; i+=3) {
verts[i] = new Vector3(Mathf.Cos(Mathf.Deg2Rad*a)*radius,0,
Mathf.Sin(Mathf.Deg2Rad*a)*radius);
a += segmentAngle;
verts[i+1] = new Vector3(Mathf.Cos(Mathf.Deg2Rad*a)*radius,0,
Mathf.Sin(Mathf.Deg2Rad*a)*radius);
}
//create triangles
for (int i = 0; i<triangles.Length;i+=3) {
triangles[i] = 0;
triangles[i+1] = i + 2;
triangles[i+2] = i + 1;
}
// generate uv cords
for (int i = 0; i < uvs.Length;i++) {
uvs[i] = new Vector2(verts[i].x,verts[i].z);
}
//lay on mesh
myMesh.vertices = verts;
myMesh.normals = normals;
myMesh.triangles = triangles;
myMesh.uv = uvs;
}
void OnCollisionEnter(Collision other)
{
Debug.Log("Something is happneing");
}
void OnCollisionStay(Collision other) {
Debug.Log ("still happening");
}
void OnCollisionExit (Collision other)
{
Debug.Log ("something left");
}
void OnTriggerEnter(Collider other)
{
Debug.Log("Something is happneing");
}
void OnTriggerStay(Collider other) {
Debug.Log ("still happening");
}
void OnTriggerExit (Collider other)
{
Debug.Log ("something left");
}
}
You should make it convex for one. Youre not supporsed to move non-convex mesh colliders. they cripple the physics system.
Not sure if that will solve your problem.
I might experiment with it tonight as im sure the parent object should be receiving all the collision events for child colliders.
hmmm good to know, i tried that and still had the same result, it just runs through the wall. The oncollisionenter does work only on the predetor object so when it hits stuff i can make stuff happen, but the predetor view won’t give me anything. Thanks for the feedback though