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?
RaycastHit2D hit = Physics2D.Raycast(...). All the parameters that have a= somethingare 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).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.upshould 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).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/…