Since castles housed the most important family and people in a kingdom or a country, the location of their home must be placed in the highest location possible. That is why many castles were built on tops of the hills; it was for the royal family’s protection. Invaders had a hard time climbing up the hill especially when they were bringing their weapons and heavy cannons.
Climbing alone the hills could drain their energy and put them at disadvantage. As for the soldiers in the castle nestled on top of the hill, they could easily see their enemies thus they could easily target them and attack them first.