I made a button that is clickable during the game playing. As I want the button have a fixed position in the main camera, I made both the main camera and the button children of the player gameObject so that the camera will follow the character while jumping or moving, everything works fine but there also a border colliders which will prevent the character moving out of the playing area. But then the collider of the button which was made intend to make the button clickable will also collide with the border which will prevent the character moving right forward. If we set the collider of the button a trigger, it seems that the button will be triggered wherever I click the mouse on the screen, that's not what I wanted.
I know maybe I could prevent this by checking if the collided object is the button or the character, but is there a better way to do that? Thanks.
For a 2D platform game, I would add a 2D user interface in a canvas over the "map" (the layer where you have the gameobjects like the character, platforms, enemies...). So the button will be always in the same place of the screen and will never collide with any gameobject of the game.
You make take some ideas from here: https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-events-and-event-triggers
Try use new UI in Unity may it fix your issue:
https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-button
Related
I have a mobile game with a simple mechanic where player must touch screen to jump. Problem is, if player clicks pause button, character jumps as well. I need to prevent this.
I came up with an idea of creating a square without sprite renderer and cover the area that I want to prevent from click and simply give a layer to that square so I can control if player is not clicking that layer, then jump.
How do I achieve this? Any help would be appreciated.
Thanks a lot.
I have a collider that when grabbed (pinch gesture), it runs a method. However, I don't want the collider react to select events. Right now, the collider prevents buttons behind it being pressed. So when the pointer is coming from the hand, the ray stops at the collider and doesn't reach to the button behind it.
From my understanding, if there is a collider, the hand interaction ray stops at it, no matter what components it has. My guess is that I need to have a layer that ignores these colliders that still allows for a grab event to be registered. But when I added a layer for the Pointing Raycast Layer Mask, it correctly ignores the collider for the far interactions, but fails to recognize a grab event.
This is how my interactable component looks:Interactable Component
I appreciate any help! Thank you
In the input settings, change the size of Pointing Raycast Layer Masks. Set UI to be element 0 so that pointing events react to that first. documentation
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.
I have a cardboard box that can be picked up by the player. I have set up a mesh collider on this box and now it keeps any items that get picked up by the other hand within. However, as soon as I teleport, the items within the box do not follow the new position. Any idea how to achieve this?
When you teleport (I assume you just change the transform position), it goes in that frame to that position, colliders and physics are ignored.
A simple way would be to make the cardboard box a child of the player as soon as you pick it up (most VR frameworks already do this), and then items that go inside the box a child of the box.
I'm learning Unity 5 and I have made simple game scene which has a coin and I wrote a code to collect a coin by first person character and when he walks towards the coin. So I dragged and dropped my code into coin layer in Hierarchy but to make this feature available I need to check the little checkbox of Is Trigger at my FPSController settings at the right side. The problem is Capsule Collider is shown at all and I really don't know why! There MUST be a section which is called Capsule Collider with Is Trigger option under the Audio Source. But as you can see ,it's not there & I don't know how to find it.. see my print screen
If it does not display in your Scene View, Enable it in the Gizmos by clicking on it.
Then under Built-in Components, make sure that Capsule Collider is selected. It show now show in the Scene View.
If it is not in the Inspector or not attached to your FPS Controller, add it by yourself.
Select the FPS Controller, Got to Component->Physics->Capsule Collider. You can then resize it.
You can add the Capsule Collider into your character yourself. All you have to do is select your character, scroll down the Inspector panel, click add component, search for Capsule Collider and select it.