- Home /
Ray Travels Incredibly Short Distance -- Unity 5.5.0f3 -- Windows 8.1 -- Solved Sort-of
Hi! I'm definitely still learning about Unity so this may be a really dumb question and I'm missing something incredibly obvious. If I am, I'm sorry but I've looked around a bit online and done some experimenting in Unity and I cannot figure out what's wrong.
I'm following a tutorial here (https://catlikecoding.com/unity/tutorials/hex-map/part-1/) about how to create a hex map. I've tried a couple other tutorials on that site and I've run into some glitches. I figure it's probably because the person making the tutorials is using all different versions of Unity and I'm not. I've been able to work most of them out but I'm stuck on this one.
The section of code that the problem is in is this one:
void HandleInput () {
Ray inputRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(inputRay, out hit)) {
TouchCell(hit.point);
}
}
If I understand what's supposed to be going on, when this function is called (whenever I click), the camera shoots out a ray towards the point that I clicked on. If it hits something, it returns that and calls the TouchCell function. The TouchCell function changes the color of the cell and it's pretty obvious that that's not happening. I did some experimenting and some online research, and went down some dead ends. It's making it up to the if statement but not making it in. I think that means my ray isn't hitting anything. I've found out out why. The ray is so tiny it's invisible to the naked eye. I've tried multiplying its direction but the only difference that multiplying it by 150 makes is a change of 0.1 in the z. I don't understand what's going on. Can anyone please explain? Thanks!
Edited: I have come to the conclusion that rays make no sense and I just don't understand what's going on. But I reached the level of desperation where you just start clicking things and seeing what happens and I found out that toggling the mesh collider being convex on and off makes the next raycast work. I don't know why, I don't know what the problem is and, frankly, as long as I can make it work, I don't care but, if anyobody knows what causes this, it would be useful to know. Thanks! Sorry for bothering you.
Answer by SynergyStudios · Aug 06, 2018 at 05:13 PM
public float range = 300;
void HandleInput () {
Ray inputRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(inputRay, out hit, range)) {
TouchCell(hit.point);
}
}
chane the values of the range variable.
this might work, but i dont know. i am an intermediate coder, so i only know some stuff. hope it works!
Your answer
Follow this Question
Related Questions
camera.ScreenPointToRay always has same origin... 1 Answer
Checking if object stopped being hit by Raycast while overlapping another object of the same type. 0 Answers
Ray. Find Point without Physic Collision! 0 Answers
Why is the camera.screenpointtoray off? 1 Answer
Problem with raycast distance 1 Answer