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");
}