Random movement

Hi,
I would like to make a game object move in a quite random way, on an irregular interval. Thus I decided I’d use the Random.Range method and a Coroutine. However, the object is moving only towards the left/bottom of the screen, never towards the right/top. Moreover, it doesn’t wait some time in a standing position as I wanted it to. Any idea why ?

Here’s the code I used :

public SpriteRenderer spriteRenderer;
    public float speed = 4;
    private Vector3 targetPosition;
    private bool isMoving = false;

    void Start()
    {
       
    }

    void Update()
    {
        if(isMoving)
        {
            Move();
        } else
        {
            SetTargetPosition();
        }
       
       
    }

    void SetTargetPosition()
    {
        StartCoroutine(WaitForMove());
        float xModifyer = Random.Range(-1, 1);
        float yModifyer = Random.Range(-1, 1);
        targetPosition = new Vector3(transform.position.x + xModifyer, transform.position.y + yModifyer, transform.position.z);
        isMoving = true;
    }

    void Move()
    {
        if (targetPosition.x < transform.position.x)
        {
            spriteRenderer.flipX = true;
        }
        else
        {
            spriteRenderer.flipX = false;
        }
        transform.position = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
        if (transform.position == targetPosition)
        {
            isMoving = false;
        }
    }

    IEnumerator WaitForMove()
    {
        while(true)
        {
            int a = Random.Range(10, 15);
            yield return new WaitForSeconds(a);
        } 
    }

The coroutine above does nothing. All of its impact is local and I’m not even sure you’re starting it in any case.

Steps to success:

  • make a timer (float), count it up by Time.deltaTime each frame

  • when it exceeds your time interval to choose a new place, then choose a new destination

  • make your character always move towards that destination (however you are moving stuff)

Also, please note for the future that scripting questions should really go in the dedicated Scripting sub-forum, not the 2D sub-forum.

Thank you for your help, now I managed to make it wait with a Time.deltaTime timer. However, the character still only moves towards the left or the bottom, which I don’t understand since the random range is from -1 to 1. Plus, it never moves in diagonal, always full left or full bottom, even though both axis are supposed to be modified by the code above. Am I missing something ?

EDIT : The problem was my use of Random.Range : I used it as a range between two integers instead of two floats. Should have been Random.Range(-1.0f, 1.0f) instead of Random.Range(-1, 1)