Unity/유니티 기본

[4.28] 캐릭터를 클릭한 지점으로 이동

ljw4104 2021. 4. 28. 15:49

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        if (this.moveCoroutine != null)
        {
            StopCoroutine(this.moveCoroutine);
        }
        var mousePosition = Input.mousePosition;
        this.moveCoroutine = StartCoroutine(this.hero.Move(Camera.main.ScreenToWorldPoint(mousePosition)));
    }

    this.gaugeGo.transform.OverlayPosition(this.hero.hudGauge.transform);
}

public IEnumerator Move(Vector2 pos)
{
    if (pos.x - this.transform.position.x < 0)
    {
        this.transform.GetChild(0).transform.rotation = Quaternion.Euler(0, 180, 0);
    }
    else
    {
        this.transform.GetChild(0).transform.rotation = Quaternion.Euler(0, 0, 0);
    }

    var dir = ((Vector3)pos - this.transform.position).normalized;
    while (Vector2.Distance(this.transform.position, pos) > 0.1f)
    {
        this.anim.SetBool("run", true);
        this.transform.Translate(dir * 3f * Time.deltaTime);

        yield return null;
    }

    this.anim.SetBool("run", false);
}

Ray를 쏘지않고 좌표를 변환하는 식으로 좌표를 얻었다.

 

반대방향을 볼 때, 껍데기 밑에있는 Model을 rotation 돌려줘야된다.

아니면 방향만 반대방향으로 보고 계속 직진하는, 문워크 하기 때문이다.

그래서 transform.GetChild(0).transform으로 첫번째 자식인 모델을 가져와서 돌렸다.