Rotating Gameobject not Colliding

When I rotate an object, the object rotates perfectly, but it just passes through anything it collides with.

 function Update(){
      transform.localEulerAngles.y += Input.GetAxis("Mouse X");
      transform.localEulerAngles.x += Input.GetAxis("Mouse Y");
 }

None of the colliders are marked as triggers, and there is nothing else going on. Just two objects next to each other, one rotating and one stationary, the rotating one passing through the stationary one. Any way to prevent this or fix it?

First make sure that you have attached rigid body to your game object which you want to rotate.

Second, Don’t use transform function because it over writes values of physics engine. By using transform.localEulerAngles you’re forcing game object to rotate despite of the colliders. Instead use
RigidBody.angularVelocity to Rotate your game object so it will collide with every collider.

So your code would look something like :

	void Update(){
		float Xval = Input.GetAxis("Mouse Y");
		float Yval = Input.GetAxis("Mouse X");
		rb.angularVelocity += new Vector3(Xval , Yval, 0);
	}

Check out this video for more understanding :