0

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?

Olivier Moindrot
  • 27,908
  • 11
  • 92
  • 91
DrakeTruber
  • 327
  • 1
  • 6
  • 21
  • 5
    Well, if it is 2D, you should also use a 2D raycast. Docs: http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html – Gunnar B. May 01 '16 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 – DrakeTruber May 01 '16 at 21:12
  • Could you perhaps give me an example of how you would use the function to achieve what I'm trying to above? @GunnarB. – DrakeTruber May 01 '16 at 21:13
  • http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html – yes May 01 '16 at 21:17
  • 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). – Gunnar B. May 01 '16 at 21:21
  • There is a code example at the docs if you didn't scroll down. – Gunnar B. May 01 '16 at 21:21
  • would the direction be "forwards" since we're talking about the object being directly under the object? By the way I've been trying all kinds of variations of the function, many directly from tutorials and they haven't been working. If it's not to much to ask, could you fill in the paramaters so that by clicking on an object we would get a raycasthit? @GunnarB. – DrakeTruber May 01 '16 at 21:29
  • 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). – Gunnar B. May 01 '16 at 21:33
  • And also what is specifically wrong with the code that I've used above that won't give me the desired actions I'm going for? – DrakeTruber May 01 '16 at 21:39
  • @GunnarB. Why would a layer not allow raycast interaction, and how would you change that? Also, why would up be the correct way? Wouldn't up be, well upwards instead of fowards? I have my Axis set up with y increasing upwards and x increasing to the right – DrakeTruber May 01 '16 at 21:42
  • 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 http://stackoverflow.com/questions/20583653/raycasting-to-find-mouseclick-on-object-in-unity-2d-games – Gunnar B. May 01 '16 at 21:55
  • As mentioned, your above code is for 3D, thats why it is not advised to use it in 2D if not even not working at all. In 3D it would work just fine. – Gunnar B. May 01 '16 at 21:59

1 Answers1

6

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.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • 2
    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. – Programmer May 01 '16 at 22:23
  • 1
    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 – DrakeTruber May 01 '16 at 22:30
  • 1
    @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! – Programmer May 01 '16 at 22:39
  • 1
    You are really a life saver! – ozgunb Sep 01 '16 at 18:41