Raycast corners of a Box Collider following rotation?

Hi everyone,

I wanted to check for ground collision correctly from all corners of a box collider.

		RaycastHit hit;

		float collyLengthX = boxCollider.size.x;
		float collyLengthY = boxCollider.size.y;
		float collyLengthZ = boxCollider.size.z;

		Vector3 cornerOrigin1 = new Vector3 (rigidbody.position.x + collyLengthX / 2, rigidbody.position.y, rigidbody.position.z + collyLengthZ / 2); 
		Vector3 cornerOrigin2 = new Vector3 (rigidbody.position.x - collyLengthX / 2, rigidbody.position.y, rigidbody.position.z - collyLengthZ / 2); 
		Vector3 cornerOrigin3 = new Vector3 (rigidbody.position.x + collyLengthX / 2, rigidbody.position.y, rigidbody.position.z - collyLengthZ / 2); 
		Vector3 cornerOrigin4 = new Vector3 (rigidbody.position.x - collyLengthX / 2, rigidbody.position.y, rigidbody.position.z + collyLengthZ / 2); 

		float rayLength = collyLengthY / 2 + 0.05f;

		Debug.DrawRay (cornerOrigin1, Vector3.down * rayLength, Color.red);
		Debug.DrawRay (cornerOrigin2, Vector3.down * rayLength, Color.blue);
		Debug.DrawRay (cornerOrigin3, Vector3.down * rayLength, Color.cyan);
		Debug.DrawRay (cornerOrigin4, Vector3.down * rayLength, Color.green);

My problem is that this doesn’t follow rotation, how can i achieve this?

Use

-transform.up

instead of

Vector3.down

Below is the working code.

    private BoxCollider colly;
    
    private void Awake () {
           colly = GetComponent<BoxCollider> ();
    }
    
    private void FixedUpdate () {
            Vector3 size = colly.size;
            Vector3 center = new Vector3 (colly.center.x, colly.center.y, colly.center.z);
    
            Vector3 vertex1 = new Vector3 (center.x + size.x / 2, center.y, center.z + size.z / 2);
            Vector3 vertex2 = new Vector3 (center.x - size.x / 2, center.y, center.z - size.z / 2);
            Vector3 vertex3 = new Vector3 (center.x + size.x / 2, center.y, center.z - size.z / 2);
            Vector3 vertex4 = new Vector3 (center.x - size.x / 2, center.y, center.z + size.z / 2);
    
            float rayLength = size.y / 2 + 0.05f;
    
            Debug.DrawRay (transform.TransformPoint (vertex1), -transform.up * rayLength, Color.red);
            Debug.DrawRay (transform.TransformPoint (vertex2), -transform.up * rayLength, Color.red);
            Debug.DrawRay (transform.TransformPoint (vertex3), -transform.up * rayLength, Color.red);
            Debug.DrawRay (transform.TransformPoint (vertex4), -transform.up * rayLength, Color.red);
    }

The key was to convert local positions to world positions with transform.TransformPoint ().