Weird error with RayCasting (CS1502)

Hello,

recently, i’ve stumbled upon a little but very annoying error. Even though i have used raycasts very often without any problem, i still can’t figure it out.

public void Attack()
	{
		swordAnim.SetTrigger ("isAttacking");


		Vector3 dir = cam.transform.forward;
		Ray ray = new Ray(cam.ViewportPointToRay (new Vector3(0.5f, 0.5f, 0)), dir);
		RaycastHit hit = new RaycastHit();

		if (Physics.Raycast (ray, out hit, 5f)) 
		{
			Debug.Log ("RayCast out!");
			if (hit.collider.tag == "Enemy") 
			{
				Debug.Log ("Enemy hit");
			}
		}
	}

No matter what i try, it always results in error cs1502.

The best overloaded method match for `UnityEngine.Ray.Ray(UnityEngine.Vector3, UnityEngine.Vector3)’ has some invalid arguments

I hope anyone can figure it out.

Thanks :slight_smile:

The error is quite descriptive. The Ray constructor expects two Vector3 parameters, as explained in the docs

But on line 7 you’re trying to construct a Ray by passing in a Ray and a Vector3:

Ray ray = new Ray(cam.ViewportPointToRay (new Vector3(0.5f, 0.5f, 0)), dir);