The point of the code:
I have this code that shatters glass using a fracture pattern
The problem:
I have noticed a problem a while back where, sometimes when I shoot the glass and thus trigger the code, the new shattered glass doesn’t get it’s position changed to where it should be when I change the rigidbody.position. This seems to happen no matter the isKinematic value as well.
Also, the code creates many pieces, and when this issue happens, it happens to every one of them which leads me to believe it could have to do with timing. I have also noticed that when adding both of the Debug.Log()s in the code the issue stops appearing.
The rigid body (in case it matters):
rigidbody.interpolation = RigidbodyInterpolation.Extrapolate;
rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative;
The code:
I am not gonna post the full code as long as not requested as I believe this includes everything important, but I could be wrong.
The following is the function where I change the position of the rigidbody.
private GameObject ConvertConvexPolygon2DToShard(Vector2[] polygon, Transform relativeTo){
//relativeTo is the glass piece being shattered, which is used here to move the newly created polygon to the correct place
if(emptyShardsIn.childCount == 0){
CreateEmptyShards();
}
GameObject shatterPiece = emptyShardsIn.transform.GetChild(0).gameObject; //Get a pooled object
shatterPiece.SetActive(true);
GlassInfo glassInfo = shatterPiece.transform.GetComponent<GlassInfo>();
Rigidbody rigidbody = glassInfo.m_rigidbody; //So that I wouldn't have to do GetComponent<T>() everywhere
rigidbody.rotation = relativeTo.rotation;
MeshFilter meshFilter = glassInfo.meshFilter;
MeshRenderer meshRenderer = glassInfo.meshRenderer;
shatterPiece.transform.parent = glassPieces;
meshRenderer.material = brokenGlassMaterial;
Mesh newMesh = new Mesh();
Vector3[] vertices = new Vector3[polygon.Length];
int[] triangles = new int[(vertices.Length-2)*3];
Vector2 addedPoints = new Vector2(0,0);
for(int i = 0; i < polygon.Length; i++){
Vector2 point = polygon[i];
addedPoints += point;
}
Vector2 pointsMedianCenter = addedPoints/polygon.Length;
rigidbody.position = relativeTo.position + relativeTo.right * pointsMedianCenter.x + relativeTo.up * pointsMedianCenter.y;
//Debug.Log(rigidbody.position); //Adding BOTH of these makes the issue stop
//Debug.Log(relativeTo.position); //When printing these out by themselves though, everything seems alright
for(int i = 0; i < polygon.Length; i++){
Vector2 point = polygon[i];
vertices[i] = Vector3.right*(point.x-pointsMedianCenter.x) + Vector3.up*(point.y-pointsMedianCenter.y); //Move the vertices to counter the rigidbody moving
}
//The following is most likely unimportant
for(int i = 2; i < vertices.Length; i++){ //Fill the polygon
int triangleStart = (i-2)*3;
triangles[triangleStart] = 0;
triangles[triangleStart+1] = i-1;
triangles[triangleStart+2] = i;
}
newMesh.vertices = vertices;
newMesh.triangles = triangles;
meshFilter.mesh = newMesh;
return shatterPiece;
}
Note that I have filtered the code a bit to get rid of completely unimportant lines such as changing the name of the object
In the code, when logging the two positions by themselves so that the problem still appears, everything seems to be fine which, again, leads me to believe towards this being a timing problem.
I have attempted to get help for this problem multiple times (in the discord server), but without success.
Also, I apologize, as this is the first time I use this forum and I might have gotten something wrong, or forgotten to include information that I should have included.
This could totally be an oversight by me, but I haven’t been able to fix this for over a week now so I have come here for help.
Linux - Unity Version 2022.3.7f1