How to detect a collision between the character and the bottom of a cube?

Hi,

I’m new to Unity and right now I’m using a capsule with First Person Controller.

I put a foating block and my script detect when the player touch the side but not the top nor the bottom of the block.

This script is attached to the FPController (C#):

using UnityEngine;
using System.Collections;

public class BlockInteraction : MonoBehaviour {
	public float pushPower = 2.0F;
	
    void OnControllerColliderHit(ControllerColliderHit hit) {
		
		Rigidbody body = hit.collider.attachedRigidbody;
        if (body == null || body.isKinematic)
            return;
        
        if (hit.moveDirection.y < -0.3F)
            return;
		
		Debug.Log("Collided");
    }

}

(Used from the docs on the Unity website);

FPControler Properties:
Transform > Scale: 1, 0.5, 1
Character Controller > Radius: 0.6
Character Controller > Height: 2

Graphics Properties:
Scale: 0.4, 0.5, 0.4

I’ve tried adding a sphere on the top of the character, tried to make the radius higher for the capsule (Someone on the internet suggested that it might resole the problem, with no success)

Any help is appreciate.

You can check the CharacterController.collisionFlags to see if you got hit from above. Or you can check the normal in the ControllerColliderHit parameter of OnControllerColliderHit() function to see if it is facing downward. You can use either Vector3.Dot() or Vector3.Angle() to compare the normal to the downward direction. Or I’ve seen it done by adding empty child object to the cube (one for each side), each with its own collider.

Thanks for your answer. It helped me out a lot. I’ve looked on the Docs of Unity to learn more about Collider, Trigger, etc and I came up with my solution.

void OnControllerColliderHit(ControllerColliderHit hit) {
        
		
		if ((hit.gameObject.tag == "SurpriseBlock") && (hit.moveDirection == new Vector3(0.0F,1.0F,0.0F) )) {
			//Hit from bottom
			hit.collider.renderer.material.color = Color.red;

		}
    }