// Update is called once per frame void Update() { if (Vector2.Distance(m_NonStaticRegion.GetDynamicPosition(), m_NonStaticRegion.GetDynamicDestination()) < 100) { //if we have reached the target. //set a new target. m_NonStaticRegion.SetDynamicDestination(new Vector2( Random.Range(differenceXMin + (m_NonStaticRegionRectTransform.sizeDelta.x / 2), differenceXMax - (m_NonStaticRegionRectTransform.sizeDelta.x / 2)), Random.Range(differenceYMin + (m_NonStaticRegionRectTransform.sizeDelta.y / 2), differenceYMax - (m_NonStaticRegionRectTransform.sizeDelta.y / 2)) )); m_NonStaticTravelTime = Random.Range(20, 30); } else { //if we have not reached the target. //Lerp move towards destination. Vector2 pos = Vector2.Lerp(m_NonStaticRegion.GetDynamicPosition(), m_NonStaticRegion.GetDynamicDestination(), Time.fixedDeltaTime / (float)m_NonStaticTravelTime); m_NonStaticRegionRectTransform.anchoredPosition = pos; m_NonStaticRegion.SetDynamicPosition(pos); //Update the current save-data's non-static region location - ready for saving. SaveManager.GetInstance().GetSelectedData().m_RegionLocation.map_x = pos.x; SaveManager.GetInstance().GetSelectedData().m_RegionLocation.map_y = pos.y; } }