# 2D Trigger problem

Hi!

I’m creating a test game where the player needs to jump from box-to-box, from bottom to top and grab the Trophy.sitting at the highest box – while dodging the random laser shots. Until now it worked fine. Then i decided to add an effect as to, when the player jumps on a box, it goes down as a result of her weight, and when she jump out off the box, the box slowly floats up to it’s original position. Now, the boxes works fine. But the player wont stand on the box while the box going down, she just plops to the ground.

The boxes contains just Colliders–set to trigger, no Rigidbodies.
The player contains both a Collider and a Rigidbody.

Here are the codes i wrote to do this, which is attached to the box prefab:

``````       private bool grlOnBox;
private bool moveBox;
private Vector2 boxInitPos;

public GameObject player;
public Rigidbody2D rb;

void Start()
{

moveBox = false;

boxInitPos = transform.position; // initial position of a given box

/// Find the player and get her Rigidbody
player = GameObject.Find ("Player");
rb = player.gameObject.GetComponent<Rigidbody2D> ();

Vector3 move = new Vector3(0, 0.2f, 0); // move speed of the boxes
//player.transform.position += move*Time.deltaTime;

}

void FixedUpdate()
{

// if the player is on the move the box down
if (grlOnBox == true) {

Vector3 move = new Vector3 (0, 0.2f, 0);
gameObject.transform.position = transform.position-(new Vector3(0,0.01f,0));
rb.AddForce (-player.transform.up/2f); // Tried to move the player down as a test hack to work around the bug, but this code has effect here. But it works outside of the if-statement
//Debug.Log(player.transform.position);

// When the player jumps off a box, move it to it's initial position
} else if (grlOnBox == false && moveBox == true) {

if(transform.position.y < boxInitPos.y)
{
Vector3 move = new Vector3(0, 0.2f, 0);
gameObject.transform.position += move*Time.deltaTime;
//Debug.Log(player.transform.position);
}

}
}

// if Player is on trigger with a box, set grlOnBox to true
void OnTriggerStay2D(Collider2D obj)
{
if (obj.gameObject.tag == "Player")
{
grlOnBox = true;

}

}

// when player triggers-out of a box, set moveBox and grlOnBox to false
void OnTriggerExit2D(Collider2D obj)
{
if (obj.gameObject.tag == "Player")
{

moveBox = true;
grlOnBox = false;

}
}
``````

Any help is appreciated. Thanks!!

omg, sorry guys, i just realized that i need to set the box trigger off, and use the OnCollision2D functions. Seems like it’s working. If anything weird happen again i’ll update the thread.

Thanks!