Make a pillar go down after activating a trigger

Hello i am fairly new to unity, but have been watching some tutorials so now i want to make my own game with its own mechanics.
But ive run into a problem.
As the title says, I have a pillar i want to move down, as soon as i enter the box collider. The pillar moves down when i enter, it just doesnt “slide” down, it only moves by 0.14 units or something, and ive set the code to be 5 units to slide, because if i do the code without the box collider set to trigger, it slides down just fine, but i want a trigger to do it… so yea… what is wrong here?

	float startingPos;
	float endPos;
	
	//units for the object to move
	public int unitsToMove = 5;
	
	//object's movement speed
	public int moveSpeed = 2;
	
	//enemy moving right or left
	bool moveDown = true;

	
	void OnTriggerEnter(Collider other)
	{

		startingPos = transform.position.y;
		endPos = startingPos + unitsToMove;

		if (other.tag == "Player") 
		{
			if (moveDown ) 
			{
				transform.position += Vector3.down * moveSpeed * Time.deltaTime;
			}
			
			if (transform.position.y <= endPos) 
			{
				moveDown = false;
			}


		}
	}
}

I assume that when you say the pillar slides down just fine when you deselect the Is Trigger option, it slides down as soon as you enter Play mode. This is because it becomes a normal physics operation, and gravity pulls it down. It’s not running any of the code above in this case. Anyway, the reason it only moves a small amount is because OnTriggerEnter() is an edge detection callback. It only happens one time, when the trigger is entered, not every frame. There is also an OnTriggerStay() method, but I don’t think this is what you want either, because if they were to leave the trigger area, the pillar would stop moving down. You just need to kick off a coroutine in the OnTriggerEnter() method instead, something like this:

void OnTriggerEnter(Collider other)
{
    if (other.tag == "Player")
        StartCoroutine(MovePillar());
}

IEnumerator MovePillar()
{
   startingPos = transform.position.y;
   endPos = startingPos + unitsToMove;

   moveDown = true;
   while (moveDown ) 
   {
     transform.position += Vector3.down * moveSpeed * Time.deltaTime;
     moveSpeed -= 9.8 * Time.deltaTime;
     yield return null;

     if (transform.position.y <= endPos) 
     {
       moveDown = false;
       transform.position.y = endPos;
     }
   }
}

I modified a bit to add acceleration due to gravity, and also you need to limit your final position so that the pillar doesn’t randomly fall a bit further below ground in some instances, depending on how long the frame lasted.

OnTriggerEnter is called on the exact same frame the collision is detected, thus one frame.
If you want it to move all the way down, you could use a Coroutine to do so.

private IEnumerator SlidePillarDown () {
   collider.enabled = false; //This line is there because I assumed you would only want to trigger this behaviour once
   while(transform.position.y > endPos) {
       transform.position += Vector3.down * moveSpeed * Time.deltaTime;
       yield return 0;
   }
}

Your OnTriggerEnter function would then look like this :

private void OnTriggerEnter (Collider other) {
    if(other.tag == "Player") {
        StartCoroutine (SlidePillarDown());
    }
}