Please tell me how best to write a script for a character’s movement in a 2D game with a top view so that the character goes around or stops in front of the buildings. The fact is that the buildings in the game are normal sprites, as well as the moving character itself and did not go to the rocks. I started writing the character's movement - when you click on the screen with a mouse, the character goes to the click, but I ran into the problem that the character passes right through the building in front of him, now I need to refine the script just won’t think of how.
void Update () { if (Input.GetMouseButtonDown(0)) { SetTarget(); } if (target && !isMove) { StartCoroutine(MoveTo(target.position)); } if (target) { StartCoroutine(delTarget()); } } public void SetTarget() { RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); if (hit) { if (hit.collider == null) return; string tag = hit.collider.tag; if (tag.Equals(HelperConst.BUILD) || tag.Equals(HelperConst.ROCKS)) { return; } if (target) { Destroy(target.gameObject); } GameObject newTarget = Instantiate(Resources.Load("Point"), hit.point, Quaternion.identity) as GameObject; target = newTarget.transform; } } public IEnumerator delTarget() { yield return new WaitForSeconds(2); if (target) { Destroy(target.gameObject); } } public IEnumerator MoveTo(Vector3 target) { isMove = true; int posWalk = HelperConst.getOnePos(transform.position, target); animator.SetInteger("pos", posWalk); float lengthLocTarget = locTarget.magnitude; float xst0 = speed * Time.deltaTime; float xst = lengthLocTarget / xst0; float posNow = 0; while (posNow < lengthLocTarget) { transform.position +=locTarget.normalized*speed*Time.deltaTime;//*locTarget.normalized; posNow += speed * Time.deltaTime; ; yield return null; } isMove = false; }