unity3d detect gameobject enters in main camera screen and leaves - unity3d

I'm looking for events or a code that will give me information that gameobject entered in the main camera screen and then leaves the screen so i can destroy it.
What i'm doing is a top down game which is moving forward all the time... some objects are spawned far away so i want them to move when they enter in the game screen and after leaving the screen destroy them.
i tried this event but it's triggered before game object enters in the screen... i saw some comments that rendering engine is triggering this event for shadow rendering bla bla...
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnBecameVisible.html

On top of OnBecomeVisible you also have other options such as OnWillRenderObject, Renderer.isVisible
You can also calculate whether an object's bounding box falls within the camera's view frustum, using GeometryUtility.TestPlanesAABB

Related

Unity specific area touch input

i want to make an area in my game where i can get touch inputs from the player.
Now i get touch from all the way of the screen.
strong textI want to get from the buttom of the screen or so.
You can make an invisible UI element or GameObject and on there check if it was clicked or not. You can do this with either https://docs.unity3d.com/2018.1/Documentation/ScriptReference/EventSystems.IPointerClickHandler.html or https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html

How to detect UI Objects from Main Camera object when using Google Cardboard VR SDK for Unity

When using Google Cardboard VR SDK for Unity, how can I detect when the Main Camera object looks at UI objects inside Canvas? OnPointerEnter() and OnPointerExit() happens when I look at 3D Objects in the samples project that Google offers, but there is no way to do it for UI Objects.
Based on your explanation, I guess you have set the Render Mode of your Canvas to Screen Space-Overlay or Screen Space-Camera. When you use one of these render modes, the position of your Canvas on the screen will never change, so you can never catch any element of the UI unless its bound includes the middle point of the screen. why? because you are using google VR and you have no joystick or something similar to control the cursor's position, therefore the cursor position is always (0, 0).
Let's assume the cursor and Canvas, both, are children of your camera. You can move your camera by shaking your head, this way the canvas and the cursor move in comparison with other objects in your virtual world based on camera movement, but they never move in comparison with each other.
So what's the solution? I think you can set the Render Mode of your canvas to World Space. This way your canvas will be an object in the world that the cursor can navigate through it. I know you think it's weird, cause likely you want to always see the canvas just in front of your eyes. So I think you just have one solution:
Do not change the Render Mode of the canvas, locate All UI elements on the canvas, it's better to locate them near the edges of the screen and also with adequate distances, then write a script to do this: When the cursor is moving, calculate the direction of movement and find out which UI elements could be the target of this movement, move it towards the middle point of the screen, when its bound includes the middle point, its OnClick will be called. After that, you must return all UI elements to their early positions. Also if the cursor stopped moving before the UI element reaches the middle point, you must return all UI elements to their early positions.
I know, it's so hard to handle this, but it's the only way I could propose.

World space button click event to take priority over the collider's click event it's inside?

Short story:
I have a button on a world space canvas with a click event handler. This is inside (in 3d space not parenting) of a 3d collider with it's own click event.
The collider always gets the click event as expected as it's nearer the camera.
I want the button to get the event.
Long Story:
I have a person mesh with a collider. You can click on them and the OnPointerClick triggers to do something.
I have a button that sits in a world space canvas which itself is located just above the mesh and is pointed towards the orthographic camera. You click on the coin and the OnClick event triggers to do something else.
Both events work as expected until the coin is inside the mesh's collider (which it is a lot of the time). At which point it's ONLY the mesh collider's OnPointerClick event triggers, not the button.
However, I ALWAYS want the button to take priority over the collider, and any other collider. This is easier when the canvas is screen space, but it's not (with reason).
How do I do this?
NOTES:
The button never gets the onclick event, so any filtering on the containing collider won't help
I've fiddled with the world space canvas and camera ray filtering settings to no effect.
The coin has to be a world space canvas for automatic tracking and because I use text too
IsPointerOverGameObject doesn't help as it's true for any collider, not just UI elements. Not to mention it wont stop the collider consuming the click anyway. A custom version of this that works via a layer wont help either, because again the OnPointerClick on the collider GO stil consumes the click.
I don't want either event to have to do any event filtering & passing on if possible, they should be atomic. Any filtering should be via setting properties in objects and inherent functionality of Unity if possible
Just to reiterate, writing a function to find all objects in the ray and then selecting ones that are on the UI layer first wont help. Because that does not change the fact that the collider still is the only thing that gets the event, which then you'd have to manually propagate down to the button.. which I don't want to do.
I've been able to fix this by putting the button's world space canvas on a sorting layer of 1. That way, even if it's behind colliders, it will register first.
Nice and clear solution that I was hoping would exist.

Cursor doesn't gaze on my UI Slider when deployed in the Hololens

Based on this repo https://github.com/qian256/ur5_unity. I am trying to get this working with some modifications, as I need. You can also see the issue in this repo.
I am unable to get the sliders move when I deploy it in the Hololens. I have a cursor that can gaze over the robot body but doesn't gaze on the slider bar or handle. I have tried most of the suggestions online including setting canvas to world-space.
I have already tried this out: HoloLens - UI/Slider and Cursor do not intersect during gaze
Without knowing more, if you can debug, set a breakpoint, and try to determine which object is being hit by your raycast, If it is an object behind your slider, then you need to adjust the layer of your slider object to be the top most layer and not shared by other game objects. If it is your slider, you might try and use the pinch and hold event, and detect which direction the user is pulling and then adjust the slider value manually.
The gaze would use Physics.Raycast thereby a Collider is required to gaze at somethiing. You can set quad primitives into your UI (with Canvas set to World Space with proper camera) by removing the MeshRenderer component but keeping the Collider one. You would then need to handle yourself gaze events for your UI e.g. gazing upper side of quad, you would manually move the slider up, etc.
Don't have 50 rep otherwise I would have commented but, I answered a very similar question here dealing with if your cursor goes through your UI element.
In the canvas I loose the cursor
However, if the problem is just that you can't move the cursor then, you need to make sure you have subscribed to the scroll/manipulation events for your cursor. You can test this to see if this is the case by just clicking on the slider in a different spot and the slider should jump to the spot you clicked.

Touch not detectable past Panel - Unity3D/Vuforia

I have been trying to add touch event to 3D gameObject in Unity.
Previously I was not using any Canvas or Panel so by using Event Trigger and Event System I was able to add touch events to the Gameobject but then I wanted to use a UI for Application and implemented this hierarchy.
Camera
GameOBject
Canvas
Panel(Transparent)
-Buttons
Panel2
EventSystem
So if i tap a part it does not respond.
Camera does have Physics Raycaster.
Gameobjects have colliders and mesh renderers.
I want a touch input from the mobile device.
Thanks
If two Button overlap each other then the order in which camera renders top will be detected.
So try changing the hierarchy order (remember the last in the hierarchy is always top of the camera ) you can achieve which one to click