Make my moving platforms pause at its endpoints using c#
I have a simple script that moves my gameobject from left to right. I want it to pause at its maximum left value and at its maximum right value before changing direction.
I tried to make a delay by calling a function that returns once Time.time has reached desired value, in this code I tried to make it wait for 10 seconds but it changes direction instantly.
How can I fix this? Appreciate any help, thanks!
public class HorizontalMovement : MonoBehaviour
{
public float moveSpeed;
public int left;
public float right;
Vector3 rightMovement = new Vector3(1.0f, 0.0f, 0.0f);
Vector3 leftMovement = new Vector3(-1.0f, 0.0f, 0.0f);
private Vector3 moveDirection;
float waitUntilTime = -1f;
void Update ()
{
move();
}
void move ()
{
if (transform.position.x >= right)
{
waitUntilTime = Time.time + 10f;
delay();
moveDirection = leftMovement;
}
else if (transform.position.x <= left )
{
waitUntilTime = Time.time + 10f;
delay();
moveDirection = rightMovement;
}
transform.Translate (moveDirection * Time.deltaTime * moveSpeed, Space.World);
}
void delay(){
if (Time.time > waitUntilTime){
return;
}
}
}
Answer by Hellium · Oct 15, 2019 at 03:44 PM
private void Start()
{
moveDirection = rightMovement;
}
void Update()
{
Move();
}
void Move()
{
if ( transform.position.x > right && moveDirection == rightMovement )
{
waitUntilTime = Time.time + 10f;
moveDirection = leftMovement;
}
else if ( transform.position.x < left && moveDirection == leftMovement )
{
waitUntilTime = Time.time + 10f;
moveDirection = rightMovement;
}
if ( Time.time > waitUntilTime )
transform.Translate( moveDirection * Time.deltaTime * moveSpeed, Space.World );
}
I believe you'd need to also update your edge conditions to involve moveDirection... unless I'm missing something this will constantly update waitUntilTime so that the Translate condition will never be reached. Otherwise, nice solution!
Yes you are right, missed this. Didn't took time to test, code fixed.
Thank you, this works perfectly. I'm Sure GrayLightGames solution is a bit more sophisticated but I went with this short one as it seems to satisfy my goal for now.
Short is great if it works! Please accept Hellium's solution so the community knows it's resolved. Good luck with your project!
Answer by GrayLightGames · Oct 15, 2019 at 03:57 PM
Hi @AttentionHorse,
WaitForSeconds may get you what you're looking for. If you want a little more control over the waiting, you can use an isWaiting boolean and a timer. Here's some example code:
public bool isWaiting;
public int moveDirection;
public float waitTimer;
public float maxWaitTime;
void Start()
{
waitTimer = 0;
maxWaitTime = 10f;
}
void Update()
{
if (isWaiting)
{
waitTimer -= Time.deltaTime;
if (waitTimer <= 0)
{ isWaiting = false; }
}
if(!isWaiting)
{ move(); }
}
void move()
{
//Translate here
transform.Translate (moveDirection * Time.deltaTime * moveSpeed, Space.World);
//Check edge condition and clamp here
if ((moveDirection == rightMovement && transform.position.x >= right)
|| (moveDirection == leftMovement && transform.position.x <= left)
{
isWaiting = true;
waitTimer = maxWaitTime;
transform.position = new Vector3(Mathf.Clamp(transform.position.x, left, right),
transform.position.y,
transform.position.z);
SwitchDirection();
}
}
The clamp will ensure that your object doesn't go beyond the left and the right position... and it would help to include the direction check in your edge condition so that after direction switches, it doesn't trigger immediately. It's also best to translate first and then check edge condition second in move() so it will happen a little smoother. Hope that helps!