Hello,
I have a scenario where I have a cross heir in the middle of my screen. If I want to see what item that my cross heir is selecting, I can simply use…
ray = pCameraObj.camera.ScreenPointToRay (Vector2(Screen.width*.5,Screen.height*.5));
getting both half the width and half the height of my game window. If I want to see what my mouse is selecting, I can use…
ray = pCameraObj.camera.ScreenPointToRay (Input.mousePosition);
I have my cross heir as a child object of my camera, and the cross heir’s local z positioning lined up very close to the camera so that no objects can render in front of my cross heir first (making a cross heir virtually appear in front of the camera at all times), but in certain scenarios of my game, where I would like to select an object and turn off my MouseLook script to interact with the different knobs and buttons of that object, I would like my cross heir’s Vector3 position to be at the x and y position of my camera view based on where the mouse pointer is in my game window, so that the cross heir in 3D space ends up directly at the mouse cursor. How would I do that?
Thank you, but what I am also looking for is a way to have the MouseLook script enabled (along with the cross heir in the center of my screen) before I ever click on an item in my game, where I am able to use…
ray = pCameraObj.camera.ScreenPointToRay (Vector2(Screen.width*.5,Screen.height*.5));
to determine what item the cross heir in the center of my screen is pointing at, which works.
After I click on an item in my game (where the ray of the centered cross heir on my screen hits the item), I want to THEN jump into, “Focus mode.” and disable MouseLook until I re-click on that item again (to toggle back into, “Mouse Look mode”). This way, when I am in, “Focus mode” my camera doesn’t turn as I am moving my mouse around as I am trying to freely interact with all the knobs and buttons on the item I just clicked on without the camera rotating in the process.
The problem I am having is that my cross heir is preferably a separate object in 3D space. It is a child object of my camera and it is extremely small since it is close to the camera. The local z position of this child object is set up to be very close to the camera so that no other objects will render in front of my cross heir. In fact, it is so close to the camera, that one decimal closer means that it would not be able to render because of the camera’s “near clipping plane”. In, “focus mode”, I would like only my cross heir’s local x and y coordinates to align with wherever my mouse pointer is in my window, based on if I had sent a ray out from my camera using…
ray = pCameraObj.camera.ScreenPointToRay (Input.mousePosition);
and put a 2D collider box at that specific z plane and got its hit coordination from there. I don’t want to put a 2D collider in front of my camera to do it this way because colliders interfere with my lighting, and I strongly feel that there just HAS TO be an easier way to do this. I tried it anyways, and got some complex miscalculations and gave up trying to find out where my cross heir was in comparison to the camera because it is so tiny.
hmm, I did something similar to this ages ago, but in my situation I had the crosshair resizing the further away the hitPoint was. The solution I had is still applicable here though, since the methods are the same, you’re just handling logic differently. So my solution was to place the cross heir on a different layer, and have it rendered last, so taking advantage of unitys culling process, then I was free to place the cross heir anywhere in the scene and it wouldn’t be culled if behind an object or inside an object etc. Then, I resized it based on distance. What you could do is very similar, you could have your cross heir in a layer of it’s own and on top, so never culled, and then render it in world where you want. So when in focus mode, you’d render it at mouse X & Y at N scale. And when not in focus mode render it at the center of the screen, at N2 Scale. Hope this makes sense, so something perhaps similar to this:
Ray ray;
if (focus)
{
ray = pCameraObj.camera.ScreenPointToRay (Input.mousePosition);
}
else
{
ray = pCameraObj.camera.ScreenPointToRay (Vector2(Screen.width*.5,Screen.height*.5));
As for the culling stuff, I made use of multiple cameras, so one with every cull mask enabled except the transparency layer I created for elements such as the cross heir. then the 2nd camera only culling for the transparency layer I created, rendered top most. And this method produced efficient framerates. Hope this helps.
That’s a good idea. Thank you, and what if my hit.transform point is null because the ray didn’t hit anything? How would I calculate where the cross heir goes then? Is there a way to calculate the scale of X and Y based on where Z is? What is the formula for that? I’m sure the camera’s field of view also has an important role in that.
In my scenario if hit.transform was null then it reset the cross heir to the center of the screen.
EDIT: Also as for Z formula, for me just scaling X and Y by Z was enough, so pseudo code wise:
Z = 0.0f;
Ray ray;
if (focus)
ray = pCameraObj.camera.ScreenPointToRay
else
ray = pCameraObj.camera.ScreenPointToRay (Vector2(Screen.width*.5,Screen.height*.5));(Input.mousePosition);
Vector3 point = ray.origin;
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
z = hit.distance;
point = hit.hitPoint;
}