Problem with raycast

I’m trying to detect if there is something between my camera and my character. Here is my code:

void Update () 
{
	myTarget = target.position + new Vector3(0, offsetY, 0);
	length = Vector3.Distance(transform.position, myTarget);
	
	//Shoot Raycasts
	RaycastHit[] hits;
	hits = Physics.RaycastAll(transform.position, myTarget, length);
	Debug.DrawLine(transform.position, myTarget, Color.yellow);

	Debug.Log("Number of hits: " + hits.Length);
}

Where myTarget is my character (I’m using an offset to point the center of it).
I’m using length for not to detect the floor.

Here is a screenshot of my scene:

Ass you can see, there is something in my raycast trajectory, but it’s not being detected… Any idea?

Make sure your objects have colliders on them. Make sure they are not in a ‘no collide’ layer.

Your problem is on line 8. You are using a world position for direction. Replace with:

hits = Physics.RaycastAll(transform.position, myTarget - transform.position, length);