title: unity restrict moving object on a plane
body: <p>I want to restrict a moving object on a plane( the plane may be or may not be a square) here is the prototype: </p>
  
  <p><a href="https://i.stack.imgur.com/kgTII.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kgTII.jpg" alt="enter image description here"></a></p>
  
  <p>I attached a mesh collider to the plane and hope to use that to restrict the movement of the sphere the sphere will move randomly, the idea is that when the ball moved to the edge the velocity will be reflected so it will not fall off the plane with this:</p>
  
  <pre><code>public class BallScript : MonoBehaviour {
  
      // Use this for initialization
      void Start () {
          GetComponent&lt;Rigidbody&gt;().velocity = new Vector3(0.1f,0,0.1f);
  
      }
  
      private void OnCollisionExit(Collision collision)
      {
          GetComponent&lt;Rigidbody&gt;().velocity = -GetComponent&lt;Rigidbody&gt;().velocity;
      }
  }
  </code></pre>
  
  <p>but the ball kept fall at the edge, </p>
  
  <p>I can't make edge colliders cause the shape of the plane will not be decided until runtime.</p>
  
  <p>Is there a way to do that?</p>