Unity OnMouseUp() triggered when object is covered bu UI element - c#

I have got some 2D elements in my scene with CircleCollider2D on them and a script with OnMouseUp(). At some stage a UI panel is rendered over the objects, and when I press a button that happens to be above the element with the OnMouseUp() it triggers both. I want to avoid disabling the circle collider every time the panel shows up and then enabling it after because of some other functionality. What can I do? Do I have to use raycasting?
Thanks for all help in advance.

The OnMouseButton() does not answer objects in the Ignore raycast layer, so change the layer between those will solve your problem, another approach is to make an event when you open and close your menu and a bool field in your character like IsClickable, then you just toggle it as the answer to UI open/close event. Hope this helps

You can check if the cursor is over UI. You can do this with the EventSystem.IsPointerOverGameObject() method, which returns a boolean. The documentation for the method.

Related

Unity Animator problem with Anchored Position

I'm currently having a problem with Unity Animator and I haven't found a solution yet. I'm trying to animate the position of a UI object through its Anchored Position with Unity Animator. Even though I have the checkbox Apply Root Motion active, when the animation ends it returns to its original position, instead of maintaining the position that changed.
I activate the animations through Triggers in a Script, and all the Scripting logic works well. The expected behaviour would be like you can see in the gif, but moving through the connectors instead of turning back to its original cell.
I tried to make an Empty GO the parent and animate child, remake the animations, remake the animator, etc., but nothing worked so far and I don't know what to try next, and I could use some help.
Thanks for answering! I will answer anything necessary when asked!
Current State Gif:
Animator Screenshot:
I can't be sure why your apply root motion is not working as it should but you can fix this in another way.
You create 3 animations with the Anchor Position, one at the startpos, one for the transition from the first one to the end one, and the endpos.
Start animation and end animation should be looping while for the transitioning one you can uncheck the looping in the inspector of animation
In the animator set the startpos as default state and create a transition with the trigger to the moving animation. Then add a transition from this animation to the idle end position without a condition but just has exit time set of the time the last animation will play
In this way, it should all work fine and if you want to come back to the first position you can add another transition with a trigger to the start or whatever you prefer.

Maximo animation i dont want my character to move with it

I have imported some Maximo animations, but when i use them i see that the character moves with the animation. when i walk for example i want it to be done via code and not the animation. how can i do this?
my animation rig is set to Humanoid, and also to loop when it ends.
i have tried to check if i can find it in the animation window, and if i can see the changes but no result. i think its the animation itself that changes its position. so please help me find this out or help me find another software where i can create an animation without moving the player.
i am talking about walking/running animations i need a way to not move it via the animation but via my character controller script.
Pretty sure you just need to turn off root motion.
https://docs.unity3d.com/ScriptReference/Animator-applyRootMotion.html
Just pick your avatar and uncheck "Apply root motion", or you can change it via code if you prefer.

Can not add click event on gameObject Unity

I want to add a click event on my gameObject, but it won't work, I don't know why it don't work on gameObject, but it work on the test button. I don't know why. Can anybody help me with it.
This is the click code:
public void cardsClick()
{
Debug.Log("aa");
}
And this is how I add click event to game object
Buttons Script
I'm not sure where you add the button script. However make sure you add it to Buttton not to Canvas.
Activate Buttons
If you add it correctly I can guess you make this cards inactive before. Then you turn it to active. In this situation Unity is bugged and buttons stop work. Try Button.Interactable instead.
Target graphics
Also if I'm not right try to add target Graphics, because Unity can don't know where is the button.
I don't have more ideas what could went wrong, but I hope I help you.

Unity: Add Button Script to 3D gameObject

I'm trying to add the Unity Button Script to a 3d gameObject. When I tried this by adding it onto a cube, I can't get the desired behaviours. How can I get the 3d object to work like a button? In particular the highlighting colors, the click events. I was speculating that perhaps it uses the wrong raycaster, and if the raycaster specificaiton is changed some how it would work? Does anyone have a good enough understanding of the source code to explain if this is possible, and if so what I would need to do to achieve this? Thanks.
Rik
You could do it by using OnMouseDown() function. Here is the link Unity Docs
You will also need to add the image script along with the button script.

Detect Input.GetMouseButtonUp outside a GameObject in Unity?

How do I detect Input.GetMouseButtonUp outside a specific GameObject's area in Unity? Are there any Unity Assets for this, or should we do this programmatically?
It certainly has to be done by programming it, whether there is an asset or not. As it is rather easy to program, I doubt there is one.
What you need to understand is, that Input.GetMouseButtonUp(int button) is an event and entirely decoupled from any "position". The description from the API says "Returns true during the frame the user releases the given mouse button.".
So in the frame a mouse button has been released you want to check if the arrow is "touching" or laying over some GameObject (GO). According to your example you will do nothing when the GO was hit, and else do something.
One way to do that is to use the following method:
http://docs.unity3d.com/ScriptReference/Camera.ScreenPointToRay.html
Your GameObject will at least need a collider, maybe a rigidbody too in order to be detected by the ray. I'm not completely sure atm if both are needed.
Additional note: Having an UI element, this method could also be used to set some flag while hovering over it e.g. : http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseOver.html

Categories