Need moving platform to fall on player enter, then return

Hi all, sorry if the answer to this question is obvious - I’ve been searching and cannot quite get it to work. Here’s my issue:

I have a platform that moves slowly up and down on its own. Once the player is on top of it, I would like the platform to move downward until the player gets off - then I would like it to return to its original position.

The code I’m using right now gets the platform to move by itself, and I can get it to the point where it will fall once the player is on top of it, but getting it to go back up is rough. If I use the below code, the player is constantly moving in and out of the trigger once on top of the platform, making it hang in place. Any help would be awesome. Thank you in advance.

Note: I’m not taking credit for this code, I found it on the forums courtesy of badbii

// declare movement variables //

var height:float = 0.35;
var up = true;
var onPlatform = false;
var speed = 0.25;


// declare position variables //

private var pos1:Vector3;
private var pos2:Vector3;

 

 
// set positions and call loop //

function Start()
{
	// set positions //
	
    pos1 = transform.position;
    pos2 = transform.position + Vector3(0,height,0);
	
	
	
	// if the balloons are not in the high position, set them there //
	
    if(!up)
        transform.position = pos2;
	
	
	// start the motion loop //
	
    StartCoroutine("Move");

}

 

 
// enter the movement loop //

function Move()
{
    while(true)
    {
    	var direction:Vector3;		// declare direction variable

   	if(onPlatform == true)
   	{
   		direction = Vector3(0,-speed,0);
   	}
   	else
   	{
   		if( up )	// if the balloons are heading upward...
		{
       		 direction = Vector3(0,speed,0);		// ...set direction's Vector to appropriate y-value, based on speed
        
       		 if( rigidbody.position.y > pos2.y ) 		// if the balloon's rigidbody is higher than the up value...
          		  up = false;								// ...send the balloons down
    	}
    	else		// if the balloons are heading downward...
    	{
    	      direction = Vector3(0,-speed,0);	// ...set direction's Vector to appropriate y-value, based on speed

      		  if( rigidbody.position.y < pos1.y )			// if the balloons rigidbody is lower than the down value...
           		   up = true;  							// ...send the balloons up
    	}
    }
        
	// move the position of the rigidbody appropriately //
	
    rigidbody.MovePosition( rigidbody.position + direction * Time.deltaTime);

    yield;
    }
}


function OnTriggerEnter(Player)
{
	onPlatform = true;
	print("ON ON ON");
}

function OnTriggerExit(Player)
{
	onPlatform = false;
	print("OFF OFF OFF");
}

Hi Orange,

I looked at your code. It looks alright, but I’m more of a C# man myself, so I might have missed something.

But it sounds like It’s a problem with the collision between the trigger and the player. Is the trigger area JUST the platform? If so, you can add an empty game object as a child to the platform → attach a trigger collider to it → then move and scale it so it fills a good area above your platform. Then, anytime the player moves into this area, it send off a trigger and STAYS triggered until he leaves. I believe this is called “compound colliders” in Unity-ese.

Cheers!
Cahman

Lol, I had a feeling I would be kicking myself once I was told the answer…

Thank you so much CahMan. Childing a separate trigger collider worked like a charm. I really appreciate your help.

Orange,

I’m glad I could help! Good luck with the game!

Cahman