# unity UGUI 移动物体的三种方法

///
/// 使用Vector3的SmoothDamp 直接进行位置更新
///
private void MoveTowards()
{
//1、获得当前位置
Vector3 curenPosition = m_elephantAni.position;
//2、获得方向
if (Input.GetMouseButton(0))
{
targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
targetPosition.z =0;
}
if (Vector3.Distance(curenPosition, targetPosition) < 0.01f)
{
m_elephantAni.position = targetPosition;
}
else
{
//  float maxDistanceDelta = Time.deltaTime * speed;
//  m_elephantAni.position = Vector3.MoveTowards(curenPosition, targetPosition, maxDistanceDelta);
float time = Vector3.Distance(curenPosition, targetPosition) / speed;
Vector3 localV3 = Vector3.SmoothDamp(curenPosition, targetPosition,ref velocity, time);
//  localV3.x = m_elephantAni.position.x;
//  LogUtil.LogManager.Log("localV3  = " + localV3.y);
if (localV3.y <= -1.2f)
{
m_elephantAni.position = localV3;
//  m_elephantAni.localScale = new Vector3(localV3.y / -6.4f,localV3.y / -6.4f,0);
}

}
}

///
/// 移动动画到屏幕中心
///
private void MoveAnimToCenter()
{
if (!ISCanMove)
return;
Vector3 curenPosition = m_elephantAni.position;
targetPosition = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width/2.0f,Screen.height/2.0f,0.0f));
targetPosition.z = 0;
targetPosition.y = curenPosition.y;

if (Vector3.Distance(curenPosition, targetPosition) <= 0.01f)
{
ISCanMove = false;
m_elephantAni.position = targetPosition;
}
else
{
float time = Vector3.Distance(curenPosition, targetPosition) / speed;
Vector3 localV3 = Vector3.SmoothDamp(curenPosition, targetPosition,ref velocity, time);
m_elephantAni.position = localV3;

}
}

public void OnPointerClick(PointerEventData eventData)
{
//LogUtil.LogManager.Log(" OnPointerClick " + eventData);
Vector2 localPoint;
//在矩形范围内检测，全局鼠标点击点，到local点的转换
RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.GetComponent<RectTransform>() , eventData.position,
eventData.enterEventCamera, out localPoint);

pointClickPostion = localPoint;

moveDirect = new Vector3(pointClickPostion.x, pointClickPostion.y, 0) - currentLocalPostion;
moveDirect.z = 0;
moveDirect.Normalize();

}
private void MoveBG()
{
if (moveBgArr != null && moveBgArr.Length > 0)
{
for (int i = 0; i < moveBgArr.Length; i++)
{
currentLocalPostion = moveBgArr[i].localPosition;
Vector3 targetPosition = moveDirect * speedBg + currentLocalPostion;
float time = Vector3.Distance(currentLocalPostion, targetPosition) / speedBg;
Vector3 localPointV3 = Vector3.Lerp(currentLocalPostion, targetPosition, time);
//  Vector3 localPointV3 = Vector3.SmoothDamp(currentLocalPostion, targetPosition,ref velocity, Time.deltaTime * time);
localPointV3.y = currentLocalPostion.y;
localPointV3.z = 0;
if (moveBgArr[i].localPosition.x +  localPointV3.x >= m_beginPanel.GetMinX() && moveBgArr[i].localPosition.x +  localPointV3.x <= m_beginPanel.GetMaxX())
{
moveBgArr[i].localPosition = localPointV3;
}

}
}
}