1

I have literally spent all day researching the light out of Unity C# Raycasting and I have nothing to show for it. I have studied tutorials, online resources, stack overflow questions, and have even word for word copied script in hopes that Unity would finally recognize all my attempts to actually use a Raycast. Here is an example of a script using Raycast that simply won't work for me:

if (mouseDown) {
    print ("mouse is down");
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit)) {
        print ("response???");
    }
}

I feel like this should work... but it's not. The mouseDown is working as it should but when I click on my object it refuses to acknowledge the rayhit from my mouse position to the object. I should also mention that the project is in 2D. Any suggestions?

12
  • 5
    Well, if it is 2D, you should also use a 2D raycast. Docs: docs.unity3d.com/ScriptReference/Physics2D.Raycast.html Commented May 1, 2016 at 21:02
  • Thanks for getting me on the right track @GunnarB. However, the parameters of the raycast function are what really confuse me, where do we place the RaycastHit variable? I'm not seeing that as one of the parameters. Also, I'm confused concerning the purpose of the layerMask as well as the min and max depth parameters. Could you clarify this for me? I really appreciate your help Commented May 1, 2016 at 21:12
  • 2
    The 2D version returns the value so you write RaycastHit2D hit = Physics2D.Raycast(...). All the parameters that have a = something are optional so you don't need to put something there if not required for your purpose. You will most like need only the start and the direction. The according line from the docs will probably be what you need (maybe the direction is not the right one). Commented May 1, 2016 at 21:21
  • 1
    It's just this line RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up);, I can't really tell what the right direction is for you because it depends on how you have setup your scene. If didn't change the camera orientation from the default one, -Vector2.up should be correct I think. If you can't get it to work, there is also the possibility that something blocks the raycast or your object isn't responding to raycast (e.g. because it doesn't have a collider or the player is on a layer that doesn't allow raycast interaction). Commented May 1, 2016 at 21:33
  • 1
    Vector2D doesn't have a forward. The axis are default like that so -up should do. Otherwise you can have a look at this question and the top two answers stackoverflow.com/questions/20583653/… Commented May 1, 2016 at 21:55

1 Answer 1

7

1.If the Object you are trying to detect touch with is an Image/Canvas, then this is not how to do this. To detect touch with Image/Canvas, you use have to derive from IPointerDownHandler or IPointerClickHandler then implement the functions from them.

public class YourClass : MonoBehaviour,IPointerDownHandler,IPointerClickHandler
{
   public void OnPointerClick(PointerEventData eventData)
   {
      Debug.Log("Clicked");
   }

   public void OnPointerDown(PointerEventData eventData)
   {
      Debug.Log("Down");
   }

}

2.Now if the GameObject you want to detect the touch with is just a 2D Texture or Sprite then use the code below:

if (Input.GetMouseButtonDown(0))
{
    Vector2 cubeRay = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    RaycastHit2D cubeHit = Physics2D.Raycast(cubeRay, Vector2.zero);

    if (cubeHit)
    {
        Debug.Log("We hit " + cubeHit.collider.name);
    }
}

For this to work, you must attach Collider2D to the 2D Texture or Sprite. Make sure that the Collider is covering the 2D Texture or Sprite by re-sizing the collider. Since this is a 2D game, any collider you are using must end with 2D.For example, there is a Box Collider and there is a Box Collider 2D. You must attach Box Collider 2D. to the Sprite/Texture.

3.If #2 did not work, then your project was created as a 3D instead of 2D. Delete the project, create a new Project and make sure you choose 2D this time. #2 answer should now work as long as a 2D collider is attached to it.

Sign up to request clarification or add additional context in comments.

4 Comments

If you look at Gunnar B.s comments, you will see why. I think you don't understand what he's been saying. To make it short, he is telling you that RaycastHit should be changed to RaycastHit2D. Also Physics.Raycast should be changed to Physics2D.Raycast. The ones with the word 2D is for 2D. You do this because your game is 2D not 3D. When you change them to 2D, you have to make a minor modification to the parameters. That's it.
True, I suppose I really didn't understand what Gunnar B. was saying until you spelled it out. I'm brand new to Unity and these simple aspects to Unity C# aren't coming as easily as I'd like. A special thanks to @GunnarB. for being patient with me
@DrakeSwartzy If you follow a Unity 2D prroject tutorial on youtube and finish it then you will be fine for the 2D side. That code is for 3D not 2D. Happy coding!
You are really a life saver!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.