Shooting a ray at an object?

I am trying to shoot a ray from one object towards another to check if there are walls between the two objects. I assumed that the origin is the object that is shooting the ray, and the “direction” I could simply substitute the transform.position of the target object. When I debug my rays with code like this, I just see them all facing on direction, and not facing towards the target object at all. I’m guessing I don’t understand what the direction actually means. So, my question is, how do I shoot a ray at a specific objects location?

My code looks like this:

RaycastHit rayHit;
		Ray rayTest = new Ray(transform.position, player.transform.position);
		Debug.DrawRay(transform.position, player.transform.position * VisionDistance,;
		if (Physics.Raycast(rayTest,out rayHit, VisionDistance))
			if (rayHit.collider.gameObject.tag == "Player")
				gameObject.renderer.enabled = false;
				gameObject.collider.enabled = false;
				gameObject.renderer.enabled = true;
				gameObject.collider.enabled = true;

Any help is greatly appreciated!

It is exactly what you thought, but your Ray is defined with its direction as “the position of the player”. Directions are (usually) not positions :slight_smile:

You want player.transform.pos - transform.pos for your direction (second argument of Ray).

edit: i can’t read

To clarify, “direction” is not “towards where should i look” but is actually “in what direction should i look”, so to get the direction to point A from point B, the formula is always:

direction = A - B