- Home /
Raycast for a 2D objects
I'm sorry to post this because I have seen others with similar posts but the reason I'm still posting it is because I'm struggling to translate it. Here's my code on a 2D object even though it's meant for a 3D and that's why I'm here.
if (Input.GetMouseButton (0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit ;
if(Physics.Raycast(ray, out hit) && hit.collider.gameObject.name =="MoveJump"){
Move.Jump ();}
Okay... Whnat do yu mean by "2D object even though it's meant for a 3D"?
Be aware that there exists a Raycast2D function for raycasting against 2D objects (those with 2d colliders).
I'm aware but I'm encountering lots of errors when I try to translate it. Heres The current code.
Ray2D ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit2D hit;
if(Physics2D.Raycast2D(ray, out hit) && hit.collider.gameObject.name == "$$anonymous$$oveRight"){
$$anonymous$$ove.Right();
===================================================================
I'm experiencing the errors cannot convert ray to ray2d and physics2d does not contain a definition for raycast2d
There's no Physics2D.Raycast2D, it is Physics2D.Raycast. Also look at the proper way to use it in the scripting api as it is different from Physics.Raycast.
http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html
Really struggling to translate because I'm an absolute beginner and hate to ask so much from but could you help me out a little more because the code used in the example seems so complex and foreign.