- Home /
I want to calculate the coordinate points of both object and make fall body in pit.
Answer by fafase · Aug 23, 2017 at 07:48 AM
You can go into math and vectors or you can use raycast.
If the black plane of your ground has a collider, then you can cast from the two top corners of your object downwards. If the raycast uses the rotation of the object it can be more accurate.
Then the hit point is the result. Convert that to local of the object and you're done.
void Update(){
Vector3 left = GetPosition(topleft.position);
Vector3 right= GetPosition(topRight.position);
}
private Vector3 GetPosition(Vector3 position){
RaycastHit hit;
Vector3 result = Vector.zero;
if(Physics.Raycast(position, mainObject.transform.down, objectSideLength)){
Vector3 pos = hit.position;
result= mainObject.transform.InverseTransformPoint(pos);
}
return result;
}
This is more pseudo code than pure solution. But hopefully it will be a lead.
The falling part, I am not sure, I guess you want to check the result of the two vectors and do something about it.
if(left.y < value && right.y < value){ Fall(); }
it does because I said it is pseudo code. I guess it tells that mainObject and ObjectSideLength are not found. Those you need to create and assign. mainObject is the game object reference of the floating object. objectSideLength is the length of the vertical side of that object. topLeft and topRight are Transform (Empty object) place at the top corners of your main object.
I just provide the logic, you still need to work out the full implementation.
Your answer
![](https://koobas.hobune.stream/wayback/20220612133334im_/https://answers.unity.com/themes/thub/images/avi.jpg)