Home > Software engineering >  I'm trying to make a movement script where the character moves from side to side and it just st
I'm trying to make a movement script where the character moves from side to side and it just st

Time:04-10

Here is my code, if it helps anyone... I don't really know what's wrong with it but when I searched online for an answer nothing helpful came up. It was all player controlled scripts and not anything I could use. Any help would be greatly appreciated, Thanks. '''

[HideInInspector] public bool moving;
[HideInInspector] public int direction;
public float restTime;
public float speed = 0.1f;
private float maxX = 2f;
private float minX = -2f;
private float currentX;
void Start()
{
    SpriteAnimator animator = gameObject.GetComponent<SpriteAnimator>();
    currentX = gameObject.transform.position.x;
    string message = "Moving: "   moving   ", Dirrection: "   direction   ", Current X: "   currentX;
    Debug.Log(message);
}
void Update()
{
    if (restTime > 0f)
    {
        restTime -= Time.deltaTime;
    }
    else
    {
        if (direction == 0 && currentX < minX)
        {
            moving = true;
            while (currentX >= minX)
            {
                currentX = gameObject.transform.position.x;
                gameObject.transform.position -= new Vector3(speed * Time.deltaTime, 0f, 0f);
            }
            moving = false;
            restTime = 5f;
        }
        else
        {
            moving = true;
            while (currentX <= maxX)
            {
                currentX = gameObject.transform.position.x;
                gameObject.transform.position  = new Vector3(speed * Time.deltaTime, 0f, 0f);
            }
            moving = false;
            restTime = 5f;
        }
    }
}

'''

CodePudding user response:

At first glance I see that if (direction == 0 && currentX < minX) should instead be if (direction == 0 && currentX > minX). Another problem that I see is that each frame would see the object be on one side or the other with no frames at positions between the two locations. If this is intended to always run with the object nearly constantly moving back and forth across the screen I would suggest trying this.

IEnumerator MoveObject() {
    while(true) {
        if(direction == 0) {
            while(currentX >= minX) {
                gameObject.transform.position -= new Vector3(speed * Time.deltaTime, 0f, 0f);
                currentX = gameObject.transform.position.x;
                yield return null; // waits for the next frame before continuing
            }
            direction == 1;
        } else {
            while(currentX <= maxX) {
                gameObject.transform.position  = new Vector3(speed * Time.deltaTime, 0f, 0f);
                currentX = gameObject.transform.position.x;
                yield return null; // waits for the next frame before continuing
            }
            direction == 0;
        }
        yield return new WaitForSecondsRealtime(5f); // how long to wait before continuing
        // If you want to allow for the "pausing" of the coroutine by modifying the passage of in-game time, you would call
        // yield return new WaitForSeconds(5f);
    }
}

And you can call it from the Start Method as follows

private void Start() {
    // Other Code Here //
    _= StartCoroutine(MoveObject());
}
  • Related