Hey, This is my first game using unity, and I am trying to make a top down shooter, Now I have shooting working and the objects being destroyed when its health gets to 0, however my raycast is going through objects, meaning I can shoot through anything to destroy my enemy cubes! Any help on how to get the Raycast to stop on the first object will be greatly appreciated!
if (CanShoot())
{
Ray ray = new Ray(spawn.position, spawn.forward);
RaycastHit hit;
float shotDistance = 20;
if (Physics.Raycast(ray, out hit, shotDistance, collisionMask))
{
shotDistance = hit.distance;
if (hit.collider.GetComponent<Entity>()) {
hit.collider.GetComponent<Entity>().TakeDamage(damage);
}