- Home /
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 :)
Answer by tanoshimi · Sep 12, 2016 at 06:41 PM
The error is quite descriptive. The Ray constructor expects two Vector3 parameters, as explained in the docs https://docs.unity3d.com/ScriptReference/Ray-ctor.html
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);
Your answer
Follow this Question
Related Questions
Can someone modify this for me? 0 Answers
Raycast doesn't collide as it should 1 Answer
How can I change this script? 0 Answers
Raycast collision on camera see's only part of object 0 Answers