How to check if object is clicked? (unity 2d) - unity3d

I used method below to check if button(object) is clicked. The problem is that I have a Confiner which is streched over the entire level and as a result method determine this Confiner but not the object that I need. So the question is can I somehow ignore confiner and only check if I clicked an object?
void OnMouseDown()
{
....
}

Implement IPointerClickHandler interface (from UnityEngine.EventSystems). This also works for 3d objects if your camera has PhysicsRaycaster component

Related

IPointerClickHandler doesn't work on unity game object

I have unity 2D project and I wanted to detect left click event on the player game object.
I added Physics2DRaycaster to MainCamera (see the first screen below)
I added two colliders to the game object (the circle collider is for detecting collisions, the box collider is triggered and used for click detection through IPointer) (see the second screen below)
I implemented the interfaces: IPointerClickHandler,
IPointerEnterHandler, IPointerDownHandler in the game object script. (see the code below)
I have EventSystem object in scene (see the third and fourth screen below)
I checked with a simple project that this combination works
But still in my project any click event isn't detecting at all. I have not idea why. I think the raycast itself doesn't work, because when I look at EventSystem logs in inspector I see that the player object doesn't detected.
UPDATE: I saw that if I switch off the canvas the IPointer gets work. But canvas elements is placed only around the player gameObject (menu buttton), but the game object isn't hided by the canvas
UPDATE 2: Ok, I figured out that even if the canvas panel is transparent it still hides the game objects. So my question is, how can I detect click on game object if the canvas pannel is in front of the game object?
This is the script of the game object:
public class Player : MovingCharacter, IPointerClickHandler, IPointerEnterHandler, IPointerDownHandler
{
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Down");
}
public void OnPointerClick(PointerEventData eventData)
{
Debug.Log("Clicked");
}
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("Entered");
}
//other code......
}
This is the MainCamera:
This is my Game Object (Player):
This is the EventSystem:
And this is the project structure:
Use layers!
If you want to be able to hit the object then put your menu on a special layer like e.g. UI and make sure the PhysicsRaycaster2D ignores that layer.
UI will be Interactable anyway since the Canvas's own raycaster already takes care of UI elements.
In general I would also make sure to only mark these elements as Raycast Target that are actually Interactable (e.g. buttons). This way an invisible background panel wouldn't block the input either.

Unity: Drag & Drop script doesn't work on my prefab gameobject but DOES work on regular UI gameobjects

I have a simple script that works just fine on any normal UI objects like an image.
But it doesn't work at all for my prefab gameobject, why is that?
I have attached a whole bunch of components to try out, like images, canvas renderer and all kinds, nothing works for me. I can also only SEE the damn prefab when I run the game, I can't see it on the Game tab. I have no clue. This is so strange.
Anyways, If I didn't make myself clear, please let me know what I need to explain or include for anyone to give me an answer?
public class DragAndDrop : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("onBegindrag");
}
public void OnDrag(PointerEventData eventData)
{
this.transform.position = eventData.position;
Debug.Log("ondrag");
}
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("onEndDrag");
}
}
Does this script only work on UI things? If so, Can I add a component to my prefab so that it is a UI item? Im working in 2d.
By default these primerily UI events are only casted onto UI elements (Image, Text, etc) which have Raycast Target enabled (using the GraphicsRaycaster component attached to the according Canvas).
If you want to be able to also use them on "normal" 3D objects you need
An EventSystem in your scene (already the case otherwise it wouldn't work for UI either)
Collider components on your objects
A PhysicsRaycaster component on your Camera!
For some reason I don't know this is only mentioned in the IPointerClickHandler (Unity 2019.1 and older) but afaik this still applies to all the EventSystem-related events / interfaces.

Two Cameras need them to face each other

I have two cameras in a scene,both following different objects I want to make them face each other ,like split screen but opposite sides,also I don't want to touch the rotation of the camera,any heads up with playing with cameras and split screening would be a great help,thanks!
This may be the thing you are looking for
// This complete script can be attached to a camera to make it
// continuously point at another object.
// The target variable shows up as a property in the inspector.
// Drag another object onto it to make the camera look at it.
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public Transform target;
void Update() {
// Rotate the camera every frame so it keeps looking at the target
transform.LookAt(target);
}
}
http://docs.unity3d.com/ScriptReference/Transform.LookAt.html
Found an easy way,created an empty gameObject rotated it upside down and inserted the second camera in it,still any other solutions are welcome and tips for multiplayer on same phone would also be great help,thanks!
Edit:This does not work.

How to change cameras in Unity based on camera coordinates?

I'd like to create a movie in Unity, so I would need several cameras and camerapaths.
On the top of this off course I'd like to change between them. For example if Camerapath1 reaches a significant point with Camera1 then I'd like to change to Camerapath2 with Camera2, etc.
I also have Camera Path Animator asset installed. It's working perfectly when I'm using it with only one camera for several camerapaths but I'm unable to change between maincameras.
I'm a newcomer to Unity. I also know that I should do something like this:
...
camera1.camera.active = false;
camera2.camera.active = true;
...
...but where should I populate these lines? On the top of this, how may I catch the event when a camera on a specific camerapath reaches a particular point?
The way to go would be an animation controller that has all camera as children and controls the active state of all cameras. This provides perfect control over the behaviour.
Create an empty game object, add all cameras as children, add an Animator to the main object with one animation. This animation takes all the camera and set their active state. One extra bonus of this approach is the possibility to call methods as well using the AnimationEvent process. You can still define within the animation for some triggered actions like explosions or movements of objects.
As I said, this give you perfect control since you can define easily actions at specific time.
The downside of it is the rigidity of the process. It may not be as flexible as code, but since you are making a movie, you probably do not need flexibility.
If so, you would have your cameras with a collider and rigidbody (isKinematic true), then you would have some trigger box with a simple script:
public void CameraTrigger:MonoBehaviour{
public GameObject nextCamera;
void OnTriggerEnter(Collider col){
if(col.gameObject.CompareTag("Camera")){
col.gameObject.SetActive(false);
nextCamera.SetActive(true);
}
}
}
Then you drag the camera meant to start next as nextCamera.

Unity3d with vuforia showing 2d image when targed is detected

I have a question about the way how to show simple 2d image on top of detected marker. I have followed some tutorial to show 3d model and it works fine. there is no problem with 3d.
The problem starts when I want to add normal 2d object->sprite . When I add simple sprite I can't add texture and when I insert UI image it's added together with canvas and it is not showed when target is
detected. The original image on editor is placed then so far that it's difficult to find it.
I would be grateful if somebody can highlight me the right direction.
I need to make this image touch sensitive like a button. Clicking into it must show new scene ( I have it but under GUI.Button). The best way is to replace original marker but I can also make new sprite bigger to hide marker under it.
To help understand the answer, here's a quick rundown on how Vuforia handles marker detection. If you take a look at the DefaultTrackableEventHandler script that's attached to the ImageTarget prefab, you'll see that there are events that fire when the when the tracking system finds or loses an Image.
These are OnTrackingFound (line 67) & OnTrackingLost (line 88) in DefaultTrackableEventHandler.cs
If you want to show a Sprite when tracking, all you need to do is put the Image Target prefab (or any other) and make the Sprite a child of the prefab. The enabling and disabling should happen automatically.
However, in case you want to do something more, here's some edited code.
DefaultTrackableEventHandler.cs
//Assign this in the inspector. This is the GameObject that
//has a SpriteRenderer and Collider2D component attached to it
public GameObject spriteGameObject ;
Add the below lines to OnTrackingFound
//Enable both the Sprite Renderer, and the Collider for the sprite
//upon Tracking Found. Note that you can change the type of
//collider to be more specific (such as BoxCollider2D)
spriteGameObject.GetComponent<SpriteRenderer>().enabled = true;
spriteGameObject.GetComponent<Collider2D>().enabled = true;
//EDIT 1
//Have the sprite inherit the position and rotation of the Image
spriteGameObject.transform.position = transform.position;
spriteGameObject.transform.rotation = transform.rotation;
And the below to OnTrackingLost
//Disable both the Sprite Renderer, and the Collider for the sprite
//upon Tracking Lost.
spriteGameObject.GetComponent<SpriteRenderer>().enabled = false;
spriteGameObject.GetComponent<Collider2D>().enabled = false;
Next, your question about detecting clicks on this Sprite. Unity's Monobehaviour fires events for a lot of mouse events, such as OnMouseUp, OnMouseDown etc.
Link to Monobehaviour on Unity's API docs
What you will need is an event called OnMouseUpAsButton
Create a new script called HandleClicks.cs and add the below code to it. Attach this script as a component to the spriteGameObject that you assigned for the above.
public class HandleClicks : MonoBehaviour {
//Event fired when a collider receives a mouse down
//and mouse up event, like the interaction with a button
void OnMouseUpAsButton () {
//Do whatever you want to
Application.LoadLevel("myOtherLevel");
}
}