var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 1000)) {
// Debug.DrawLine (ray.origin, hit.point);
mouse_icon.transform.position = hit.point;////ray.GetPoint(hit.point);
// Debug.Log(hit.point);
}
That works correctly. There are no problems with it that I see.
It puts mouse_icon where my mouse is. It also puts it in the game space in the right spot.
But I'm confused how that is doing it.
If I remove that “if” in it, it no longer works. Yet I didn’t set a variable in it.
To expand : So doing that 'if' statement controls where 'hit' goes? If I do another 'if' statement, would it reset 'hit' to the new info?
1 Answer
1
You really have to be more specific than "it no longer works" - that doesn't tell me what part of it you expected to work and how. When you say you removed the if statement, I don't know if you mean that you simply removed the if or that you completely removed the raycast as well.
See the docs on Physics.Raycast;
You did set a variable: hit. The `hitInfo` parameter is specified with the out keyword. The variable is being set by the raycast. If you remove the raycast completely, `hit` will be null;
`Physics.Raycast` returns true if it hit something (see the docs). If you don't check whether it hit something, when you don't hit anything, what do you thing `hit` is going to contain? `hit` will likely be null or contain default values.
Since you're using `hit`, when it has an invalid value, what do you think will happen when you assign `mouse_icon.transform.position` to `hit.point`? If `hit` is null, then it shouldn't do anything and if it contains some default values, then it won't do anything meaningful.
You don't really, but the docs have it, specifying the parameter as a return by reference.
– anon16733337