Drag and Drop on a certain area like mountain unity3d - unity3d

I want to drag and drop an object to a particular place only like mountain. but if the user will drop the object outside the mountain it should come back to its initial position.
I already have a script on the possibility that the user will drop the object outside the mountain.. My question is that how would I be able to only drop the object specifically on the mountain only..
I tried using the OnTriggerEnter() but I don't know how to do it..
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class mountainAreaScript : MonoBehaviour {
void OnTriggerEnter(Collider collider){
if(collider.gameObject.tag == "mount"){
//Debug.Log ("working");
}
}
void OnTriggerExit(Collider collider){
if(collider.gameObject.tag == "mount"){
//Debug.Log ("working but false");
}
}
}

Give the object your dragging a rigidbody. If needed set the is Kinematic to true to avoid collision issues with other objects.

Related

How do you switch scenes in Unity 2D on collison

I was wondering how I can switch scenes in my 2D Unity game. I put the scenes in the build and the objects are entering in colison. I used this code but it didn't worked:
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneSwitch : MonoBehaviour
{
[SerializeField] private string WhatScene;
void OnTriggerEnter(Collider2D other)
{
if (other.CompareTag("Player"))
{
SceneManager.LoadScene(WhatScene);
}
}
}
Can you help me?
https://i.stack.imgur.com/rpg3t.png
https://i.stack.imgur.com/HoOfl.png
https://i.stack.imgur.com/9GEgi.png
Note that there is
OnTriggerEnter2D(Collider2D) which as parameter takes a Collider2D, not Collider. This one is for 2D physics using Rigidbody2D and Collider2D components.
OnTriggerEnter(Collider) which takes a Collider, **not Collider2D. This one is for 3D physics using Rigidbody and Collider components.
Since you are talking about 2D your method should look like
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
SceneManager.LoadScene(WhatScene);
}
}
If you are using OnTriggerEnter method in unity2D there is a problem, you need to set collision as a trigger and use "OnTriggerEnter2D" method.
In build settings you have to add scenes, there will be a different index attributed to every scenes.
private void OnTriggerEnter2D(Collider other)
{
if (other.CompareTag("TAG"))
{
SceneManager.LoadScene([INDEX OF THE SCENE YOU WANT TO USE]);
}
}
If you want to detect a collision use OnCollisionEnter2D.

Teleporting PacMan to other side of the Maze

I'm creating a teleporting for my PacMan game to get to the other side of the maze when triggered. I have the code then but when colliding nothing happens. I need that when i trigger the Left Portal i go to the right portal. I am thinking that the Pathfinding i have might be the problem. Thanks
Portal Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Portal : MonoBehaviour
{
public Transform warpTarget;
void onTriggerEnter2D(Collider2D other){
Debug.Log("An Object Collided");
other.gameObject.transform.position = warpTarget.position;
}
}
Maybe you should check for the methods's name. It must start with an uppercase char instead of an lowercase, so it should be something like this:
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("An Object Collided");
other.gameObject.transform.position = warpTarget.position;
}

Unity - Text object instancing at wrong position

I'm instancing a small UI canvas with text to appear above a pick up on contact:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickUpCheck : MonoBehaviour {
public GameObject pickUp;
public GameObject textBox;
public GameObject particle;
private Vector3 pickUpPos;
// Use this for initialization
void Start () {
pickUpPos = pickUp.transform.position;
}
void OnTriggerEnter2D(Collider2D collider)
{
Instantiate(particle, pickUpPos, new Quaternion());
Instantiate(textBox, pickUpPos , new Quaternion());
Destroy(pickUp.gameObject);
}
}
The canvas and text appear somewhere else though. Specifically, at the position of the text object I originally reference in the editor.
I'm not sure why this is overriding the position set in the instance code. I've moved the referenced canvas object prefab around (including to the origin) and re-referenced it to the pick up object and it always will appear at this position instead of the pick up position.
Edit - Just to clarify, pickUp is the game object this script is attached to. The GameObject particle is a particle effect set to instance when the object is collided with. It is unrelated to the current problem.
You're setting the position on Start() which means that it'll get set once when you start running the game.
Have you tried setting it when you need the current position, like this?
void OnTriggerEnter2D(Collider2D collider)
{
pickUpPos = pickUp.transform.position;
Instantiate(particle, pickUpPos, new Quaternion());
Instantiate(textBox, pickUpPos , new Quaternion());
Destroy(pickUp.gameObject);
}
So I found a simple solution to the problem - I assigned the instance of the created object to a variable and then set the position of that variable to the position of the pick up object:
void OnTriggerEnter2D(Collider2D collider)
{
pickUpPos = pickUp.transform.position;
textBox = Instantiate(textBox, pickUpPos , new Quaternion());
textBox.transform.position = pickUpPos;
Destroy(pickUp.gameObject);
}
Works as intended now.

Google Cardboard - How to detect focus on an object?

I try to create a VR scene in unity using google cardboard sdk. I add a cube and CardboardMain.prefab to scene. There is an example scene that detect focus on cube. Its hierarchy view is :
I don't know how to add GUIReticle object or prefab like as the image.
How can I detect focus on an object?
Actually you could make the script by your own and it is quite simple.
You could detect whether the user is looking at your object or not by using RayCast from the Main Camera. If the RayCast hit your object, then it is being focused on.
For example:
using UnityEngine;
using System;
[RequireComponent(typeof(Collider))]
public class LookableObject : MonoBehaviour {
[SerializeField]
Transform cam; // This is the main camera.
// You can alternately use Camera.main if you've tagged it as MainCamera
bool focus; // True if focused
Collider gazeArea; // Your object's collider
public void Start () {
gazeArea = GetComponent<Collider> ();
}
public void Update () {
RaycastHit hit;
if (Physics.Raycast (cam.position, cam.forward, out hit, 1000f)) {
focus = (hit.collider == gazeArea);
} else {
focus = false;
}
}
}
Edit: This is just an example. You probably would want to make a script to do the Raycast only just once instead of doing the Raycast on each of your object over and over again to make your project runs faster.

Cannot call IVirtualButtonEventHandler Vuforia 5.0.10

Im using Unity 4.7.0 and Vuforia 5.0.10, i cannot call the IVirtualButtonEventHandler.
using UnityEngine;
using System.Collections;
public class VBEventHandler : MonoBehaviour, IVirtualButtonEventHandler
{
}
I just came across this, hope you're still using Unity & Vuforia. You need to add using Vuforia to make the call.
using UnityEngine;
using System.Collections;
using Vuforia;
Register the Virtual Button:
To add a virtual button to an image target, add the VirtualButton element and its attributes to the ImageTarget element in the .xml file.
XML Attributes:
Name - a unique name for the button
Rectangle - defined by the four corners of the rectangle in the
target's coordinate space
Enabled - a boolean indicating whether the button should be enabled
by default
Sensitivity - HIGH, MEDIUM, LOW sensitivity to occlusion
After Registering Virtual Button Code is simple then:
using UnityEngine;
using System.Collections;
using Vuforia;
public class Custom_VirtualButton : MonoBehaviour, IVirtualButtonEventHandler
{
// Use this for initialization
void Start () {
// here it finds any VirtualButton Attached to the ImageTarget and register it's event handler and in the
//OnButtonPressed and OnButtonReleased methods you can handle different buttons Click state
//via "vb.VirtualButtonName" variable and do some really awesome stuff with it.
VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour>();
foreach (VirtualButtonBehaviour item in vbs)
{
item.RegisterEventHandler(this);
}
}
// Update is called once per frame
void Update () {
}
#region VirtualButton
public void OnButtonPressed(VirtualButtonAbstractBehaviour vb)
{
Debug.Log("Helllllloooooooooo");
}
public void OnButtonReleased(VirtualButtonAbstractBehaviour vb)
{
Debug.Log("Goooooodbyeeee");
}
#endregion //VirtualButton
}