控制数字继续上下移动并且始终朝向摄像机

 

private bool UpOrDown = true;

private Transform machineIDRoot;
private List<GameObject> stroeMachineID = new List<GameObject>();

//存储机器编号
public void StoreMachineIDShow()
{
  //发现机器编号根节点
  machineIDRoot = factory.transform.FindChild("MachineIDRoot");
  foreach (Transform obj in machineIDRoot)
  {
    if (!stroeMachineID.Contains(obj.gameObject))
    {
      stroeMachineID.Add(obj.gameObject);
    }
  }
}
//机器编号的动画及其控制
private void MachineIDAnimControl()
{
  foreach (GameObject obj in stroeMachineID)
  {
    obj.transform.LookAt(new Vector3(Camera.main.transform.position.x, obj.transform.position.y, Camera.main.transform.position.z));
    if (UpOrDown)
    {
      obj.GetComponent<Transform>().DOLocalMoveY(38, 3);
    }
    else
    {
      obj.GetComponent<Transform>().DOLocalMoveY(33, 3);
    }
    if (Vector3.Distance(obj.transform.localPosition, new Vector3(obj.transform.localPosition.x, 38, obj.transform.localPosition.z))<0.1f)
    {
      UpOrDown = false;
    }
    if (Vector3.Distance(obj.transform.localPosition, new Vector3(obj.transform.localPosition.x, 33, obj.transform.localPosition.z)) < 0.1f)
    {
      UpOrDown = true;
    }
  }
}

 

另一种方法:

GameObject facilityIDParent;
List<GameObject> storeFacilityId = new List<GameObject>();;

private void SetFacilityIDShowString()
{
  storeFacilityId.Clear();
  facilityIDParent = factory.transform.FindChild("facilityIDParent").gameObject;
  foreach (Transform kk in facilityIDParent.transform)
  {
    storeFacilityId.Add(kk.gameObject);
    kk.GetComponent<TextMesh>().text = dataLogicControl.StoreFacilityData[kk.name].id;
  }
}

private void FacilityIDAnimationController()
{
  foreach (var kk in storeFacilityId)
  {
    kk.transform.LookAt(new Vector3(Camera.main.transform.position.x, kk.transform.position.y, Camera.main.transform.position.z));
    if (UpOrDown)
    {
      kk.transform.localPosition = Vector3.Lerp(kk.transform.localPosition, new Vector3(kk.transform.localPosition.x, 40, kk.transform.localPosition.z), Time.deltaTime);
    }
    else
    {
      kk.transform.localPosition = Vector3.Lerp(kk.transform.localPosition, new Vector3(kk.transform.localPosition.x, 35, kk.transform.localPosition.z), Time.deltaTime);
    }
    if (Vector3.Magnitude(kk.transform.localPosition - new Vector3(kk.transform.localPosition.x, 40, kk.transform.localPosition.z)) < 0.1f)
    {
      UpOrDown = false;
    }
    if (Vector3.Magnitude(kk.transform.localPosition - new Vector3(kk.transform.localPosition.x, 35, kk.transform.localPosition.z)) < 0.1f)
    {
      UpOrDown = true;
    }
  }
}

posted @ 2017-08-10 14:55  U3DEngineer  阅读(434)  评论(0编辑  收藏  举报