Unity Freezing when using this script

This is a code i wrote to make a gameobject move randomly around the map. however, when using this script, unity freezes and i have to close it using task manager.

How could i resolve this issue?

public class kuhmovement : MonoBehaviour
{
    public Vector2 topright;
    public Vector2 bottomleft;
    Rigidbody2D rb;
    Vector3 dir;
    float moveSpeed = 5f;
    Vector2 randompunkt;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        Punktneusetzen();
    }

    void Update()
    {
        randomrumlaufen();
    }

    void randomrumlaufen() 
    {
        Vector2 pos = transform.position;
        while(pos != randompunkt) 
        {
            dir = randompunkt - pos;
            rb.MovePosition(transform.position + dir * moveSpeed * Time.deltaTime);
        } 
        if(pos==randompunkt)
        {
            
        }
    }

    void Punktneusetzen()
    {
        randompunkt = new Vector2(x: Random.Range(bottomleft.x, topright.x), y: Random.Range(bottomleft.y, topright.y));
    }
}
  1. Using while inside update is usually a bad idea: Everything else stops running until the while is completed. Because update always runs use an if statement.

  2. Add as an argument to MovePosition the final destination you want

  3. Because of floating point precision two floats will not be the same, == operator is overloaded for Vector2 so it accounts for that, but even then because of the movement in update, you have to create an epsilon value where you check for the difference in you position and the final position

  4. Movement is better to be done in FixedUpdate

From the above, try you code like this:

using UnityEngine;

public class kuhmovement : MonoBehaviour
{
   public Vector2 topright;


   public Vector2 bottomleft;

   private const float EPSILON = 0.05f;
   
   Rigidbody2D rb;

   Vector3 dir;

   float moveSpeed = 5f;

   Vector2 randompunkt;

   private void Start()
   {
      rb = GetComponent<Rigidbody2D>();
      Punktneusetzen();
   }

   void FixedUpdate()
   {
      randomrumlaufen();
   }

   void randomrumlaufen() 
   {
    
      Vector2 pos = transform.position;
      if((randompunkt - pos).sqrMagnitude >= EPSILON) // Check if near position
      {
         rb.MovePosition(pos + moveSpeed * Time.fixedDeltaTime * (randompunkt - pos).normalized);
      } 
      else // At position
      { 
         transform.position = randompunkt;
         // Rest of code here 
      }
   }

   void Punktneusetzen()
   {
      randompunkt = new Vector2(x: Random.Range(bottomleft.x, topright.x), y: Random.Range(bottomleft.y, topright.y));
   }
}

I’m not too sure, but I think it has to do with you assigning the direction when it’s run only once, and when the position reaches that it’s told to do something but doesn’t know what.

I think if you just add ‘Punktneusetzen()’ into the ‘if(pos==randompunkt)’ it should work fine

You are probably getting an infitine loop because of your while argument. If the position doesn’t have to be accurate, rather than using while(pos != randompunkt) you can try While(Vector2.Distance(pos,randompunkt)< 0.01f) you can tweak the 0.01f value as you please. It might be a floaty point problem. Because pos is not perfectly equal to randompunkt it is causing an endless loop. Do test and let me know if it works.

Thanks everyone! it was in fact the while loop that was causing an infinite loop and causing everything to crash. thanks for your help!!!