- Home /
How to setup collision with freshly generated mesh and prefabs
The setup: There are dots(8) that spawn randomly just in front of a plane on start. The player then draws three dots with mouse-clicks. The dots connect when created, and generate a triangle(mesh) between the three dots when the third is instanced.
The desire: collision detection between the dots and the triangle. in other words, I want to award the player with the dots they've outlined with his/her three clicks and not dots outside of the triangle.
I've been reading through tutorials and answers and tried copying and pasting all sorts of code and can't get anything to trigger with the mesh. The dots will all collide (or trigger for that matter) with the plane if I ask them to, but not the mesh. They are clearly intersecting in the scene.
Is it because of the way I'm using mouse-click points in game-time to generate the mesh? perhaps I am not applying the mesh data to its collider properly? or perhaps the way to mesh just appears all the sudden it's not passing through the proper collision checks? it's going right through the dots? I am not checking isTrigger on the dots when I try collision, though on when trying OnTriggerEnter/Stay() etc. I do have a rigidbody on the dots, they do collide with the wall if I turn it's meshcollider on.
on the triangle/mesh:
void OnCollisionEnter(Collision dot){
Debug.Log ("collision on tri");
}
public void CreateTriangle() {
Mesh mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
mesh.Clear();
mesh.vertices = _vertices;
mesh.uv = _UV;
mesh.triangles = _triangles;
mesh.normals = _vertices;
_active = true;
GetComponent<MeshCollider>().sharedMesh = mesh;
// MeshCollider meshc = gameObject.AddComponent(typeof(MeshCollider)) as MeshCollider;
// meshc.sharedMesh = mesh;
// mesh.RecalculateNormals();
}
on the dots:
void OnCollisionEnter(Collision tri) {
Debug.Log ("Collision on" + gameObject.name + "with " + tri.ToString());
}
Thanks for any insight. Very new to this level of programming, but if I can get this one last step to work I can get everything else rolling =D
I understand why primitives would work a lot faster/easier etc. but I can't imagine how to draw primitives based on the triangle input the game is based off... the whole principle is connecting vertexes. eventually the player get to make 4 dots, 5 dots, etc., each checking against the rest to see if it can make any new triangles. If anyone can think of a better way to go about that than I am trying, I'll gladly consider it =D
maybe I should raycast from the dots and see if they hit with the triangle?
I got a camera projected raycast on click to intersect with the triangle.. so it's collider works... just not with the dots?? must be the way they are instanced on top of one another?
Answer by malkere · Apr 03, 2014 at 08:55 AM
well no answers yet. but I did get raycast to collide with the triangle face. solved via bypassing mesh/primitive collision and just using the primitives locations to cast rays at the one-sided, single face mesh.
I assume the standard collision process isn't working because of the way it's just instantiating the mesh right within some of the dots. If I cast a ray from the camera it collides with the triangle face, so I scripted it to fire and test a ray from each of the dots locations and provide feedback which both lets me create a unique and precise set of triangles each run and still know exactly if any of the dots are caught within it or not.
I got hung up for quite a while before I learned Vector3(0,0,-1) for the ray.direction to get the ray firing not just from where I wanted it, but to where I wanted it.
Your answer
Follow this Question
Related Questions
Implementing Meshless Deformation 0 Answers
Cant walk through doorway mesh created in Blender 1 Answer
Generated mesh collider on a generated mesh becomes a big box instead of taking mesh's shape 1 Answer
how can I instantiate an object specifically at the intersection of a collider and a mesh 1 Answer
MeshCollider Failure at high velocities 0 Answers