- Home /
Physics.Raycast to Physics2D.Raycast?
I'm editing a script that was made for Unity 3D, and need to make it work for 2D. I've got it all pretty good besides an overloaded method error. Here's the part thats giving me trouble...
if (Physics2D.Raycast(Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0.5f)), out hit, 5f))
{
if (hit.collider != null)
{
if (hit.collider.GetComponent<UI_Item>() != null)
{
go = hit.collider.gameObject;
displayMessage = true;
}
else
{
displayMessage = false;
go = null;
}
}
else
{
displayMessage = false;
go = null;
}
}
I'm pretty new to C#, and coding in general, so any help is awesome! Thanks :).
Answer by Cherno · Jun 22, 2015 at 05:06 PM
A look at the Unity Scripting API tell me that the Physics2D.RayCast function takes a Vector2 as a parameter, as opposed to the Vector3 you try to pass it. The other parameters are listed on the page as well. I am under the impression that the 2D version is a lot less flexible when it comes to parameter options, but that just as a side node ;)
Your answer
Follow this Question
Related Questions
Help with 2D physics script 1 Answer
Character Controller 2D physics or raycast ?? 1 Answer
Reduce Physics2D Lag 0 Answers