- Home /
OnTriggerStay2D not working
I am currently prototyping some things and one of them is to have objects that can be dragged and dropped into a box.
I have gotten the mechanics working for dragging the objects around and then snapping them to the centre of an area provided they have passed through the area's collider.
However what I'm having trouble with is if I have more than one of the same type of object. They will individually snap OK, but I can't seem to figure out a way of making it so once an object has snapped into a box, no more objects can snap into the box until it's empty again.
I am trying to make my code as neat as possible and have managed to get the logic down from multiple files into just 2. I did read another question where it was suggested to use OnTriggerStay2D to turn off the collider for the duration, but what seems to be happening is that when the object snaps into the box, an OnTriggerExit2D call is being made, despite the object being in the centre of the bounding box.
I'm fairly new to Unity so I'm happy for suggestions on making my existing code more efficient if possible.
slotBehaviour.cs:
using UnityEngine;
using System.Collections;
public class slotBehaviour : MonoBehaviour {
private bool slotEmpty;
public static Vector2 box;
public static bool collided;
public static string boxTag;
// Use this for initialization
void Start () {
slotEmpty = true;
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D (Collider2D col) {
if (col.gameObject.tag == gameObject.tag && slotEmpty == true) {
Debug.Log (col.gameObject.tag + " Entered");
boxTag = gameObject.tag;
collided = true;
box = gameObject.transform.position;
}
}
void OnTriggerStay2D (Collider2D col) {
Debug.Log ("Still here...");
}
void OnTriggerExit2D (Collider2D col) {
Debug.Log (col.gameObject.tag + " Exited");
collided = false;
}
}
componentMovement:
using UnityEngine;
using System.Collections;
public class componentMovement : MonoBehaviour {
private Vector2 componentPosition;
private Vector2 offset;
private bool inSlot;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnMouseDrag() {
Vector2 curScreenPoint = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
Vector2 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint);
transform.position = curPosition;
if (gameObject.rigidbody2D.isKinematic == true) {
gameObject.rigidbody2D.isKinematic = false;
inSlot = false;
}
}
void OnMouseUp() {
if (inSlot == false && gameObject.rigidbody2D.isKinematic == false) {
if (slotBehaviour.collided == true && slotBehaviour.boxTag == gameObject.tag) {
transform.position = slotBehaviour.box;
gameObject.rigidbody2D.isKinematic = true;
inSlot = true;
}
}
}
}
Please go back and edit the post so your code is formatted correctly.
Your answer
![](https://koobas.hobune.stream/wayback/20220613151649im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D shooter, 3d or 2d colliders? 2 Answers
Spike Player Die 0 Answers
Generating a pixel-perfect collider for an object at runtime 1 Answer
Is this normal behavior for edge collider triggers (2D)? 1 Answer
OnCollisionEnter2D not calling properly 2 Answers