- Home /
 
 
               Question by 
               Kubies · Mar 30, 2018 at 05:33 AM · 
                boundsmatrix4x4matrix4x4.trs  
              
 
              How can I check if a point(Vector3) is in Matrix4x4
I'm placing random objects in an imaginary cube in front of my target. I am using following code to do this:
 var center = Vector3.zero;
 center.z = (m_size.z / 2) + m_offset.z;
 center.x += m_offset.x;
 center.y += m_offset.y;
 var mat = Matrix4x4.TRS(m_target.position, m_target.rotation, m_target.lossyScale);
 Vector3 pos = center + new Vector3(Random.Range(-m_size.x / 2, m_size.x / 2), Random.Range(-m_size.y / 2, m_size.y / 2), Random.Range(-m_size.z / 2, m_size.z / 2));
 pos = mat.MultiplyPoint(pos);
 m_instantiatedObject.Add(Instantiate(m_object, pos, Quaternion.identity));
 
               And it's working as expected. but now my problem is as my target moves, the previously placed objects may not be in this imaginary cube anymore. how can I check if my placed objects are in this imaginary cube and if they are not I can remove them?
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by BastianUrbach · Mar 30, 2018 at 06:50 AM
"Undo" the transformation by multilying with the inverse of the matrix (mat.inverse) and check if the result is inside your reference cube. To check if a point is in an axis aligned cube, you either just check all vector conponents by yourself or you can use the Bounds struct to do it for you:
pos = mat.inverse.MultiplyPoint(pos);
if (!new Bounds(center, m_size).Contains(pos)) {
   // do some stuff
}
 
              Your answer