- Home /
Need help understanding ScreenPointToRay
Hello friends and coders,
I am trying to instantiate a cube on the ground where I click with my mouse, but running into troubles. Its a third person game, so I am trying to raycast from my mouse click instead of from the head of my character like in a first person game.
I have been reading up on ScreenPointToRay, and I think it does what I want, but not sure.
My question is - does using ScreenPointToRay send a raycast from my camera towards the point where I clicked in the scene?
If so - any reason why this code wouldnt work?
if(Input.GetButtonDown("Place Block") && placeBlock == true) //this will need changed
{
vRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(vRay, out hit,/* range, */ constructionLayer))
{
Debug.Log ("raycast has hit " + hit.transform.name);
addingToConstructionLayer = true;
Its obviously not the whole code but I am trying not to overload my question with unnecessary code if at all possible. By the way, I get no errors, but nothing happens and the debug statement doesnt play.
Answer by Owen-Reynolds · Dec 02, 2012 at 04:17 PM
The overload with `(Ray, RaycastHit, ...)` takes distance as the 3rd input. You've got the layer as input 3, so it's using the layer you want to hit as the distance to shoot.
You're got a commented out range in there, just plug in 9999 (or Mathf.infinity)?
Getting the layers set up correctly can be tricky. Try it without the layerMask (just leave out contructionLayer) to be sure it's doing what you think. When you can mouseOver a rock and it prints "hit rock," add the layer. To avoid a giant scrolling list, can replace the Debug.Log with `hitWord=hit.transform.name` where hitWord is a global.
Thanks - this ultimately led to me getting it to work. I originally had the range set to 100 but maybe setting it to 9999 fixed it. Its working now, thanks!
Answer by CostelloNicho · Dec 01, 2012 at 11:21 PM
This line interests me:
if(Input.GetButtonDown("Place Block") ..
Unless you have changed your input scheme you should be using (input.GetButtonDown("Fire1")) which checks for mouseOne being clicked. (This may be trivial but I'm not sure how you set it up.) I'm guessing this is the problem because you have no luck with the debug.
If it is what I am assuming, you are trying to make sure it hits something with a tag of "Place Block", Then you would want to search tags after doing the Raycast to make sure the tag is correct:
If (hit.collider.tag == "Place Block") ....
Thanks for the reply. That input is actually set to mouse 1, or right click via the input manager. I could remove it for simplicity sake for testing. But basically what is supposed to happen is when I right click the raycast is sent and if it hits the box is instantiated there.
I don't have any errors, but since nothing is happening I assume it's because my raycast isn't hitting anything.
I think more than anything I really just need to understand what screenpointtoray does so I can understand why it doesn't hit anything.
Your answer
![](https://koobas.hobune.stream/wayback/20220613084821im_/https://answers.unity.com/themes/thub/images/avi.jpg)