public void stopWalking() { isWalkingTo = false; following = false; onMonsterReachHandler -= followContinue; if (onMonsterStopWalkingHandler != null) { onMonsterStopWalkingHandler(); } }
// Update is called once per frame void Update() { if (this.GetComponent <MonsterStatus>().isAlive) // monster ต้องยังไม่ตาย { if (!following) // ไม่ได้กำลัง Following { if (isWalkingTo) // กำลังเดินอย่างมีจุดหมา { int direction = findDirect(); //หาทิศทางระหว่าง monster กับ เป้าหมาย if (distance() <= 1) // ถ้า ระยะห่างจากเป้าหมายน้อยกว่าหรือเท่ากับ 1 // reach to target point; { isWalkingTo = false; // set isWalkingTo ให้เป็น false anim.SetBool(walkHash, false); // set walkHash ให้เป็น false if (onMonsterReachHandler != null) { onMonsterReachHandler(); // สั่ง onMonsterReachHandler ให้ทำงาน } } else { // walk to target point; // สร้างเส้นสีน้ำเงินจาก ตำแหน่ง monster ไปยังจุดที่จะเดินไป Debug.DrawLine(this.transform.position, targetPos, Color.blue); anim.SetBool(walkHash, true); //set walkHash ให้เป็น true walk(direction); // สั่งให้ monster เดินไปยังทิศทางของ target } } } else // ถ้าเกิด Following อยู่ { if (PlayerStatus.Instance.isAlive) // ถ้า player ยังไม่ตาย { targetPos = target.transform.position; // หาตำแหน่ง ของ target int direction = findDirect(); // หาทิศทางระหว่าง monster กับ target if (distance() <= 3) { // monster เข้าใกล้ในระยะ 3 ช่อง following = false; move(5 * direction); // เดินไปยิงทิศทางของ Target 5 ช่อง anim.SetBool(walkHash, false); // กลับสู่โหมด move ปกติ ไม่ต้อง Continue เพื่อให้เดินชนไปข้างหน้า onMonsterReachHandler += followContinue; // เพิ่ม Follow Continue ซึ่งทำให้กลับมา follow Target ใหม่อีกครั้งหลังจากเดินไปถึงจุดเป้าหมายแล้ว } else { // walk to target point เดินไปหา target // สร้างเส้นสีน้ำเงินจาก ตำแหน่ง monster ไปยังจุดที่จะเดินไป Debug.DrawRay(this.transform.position, targetPos); anim.SetBool(walkHash, true); //set walkHash ให้เป็น true walk(direction); // สั่งให้ monster เดินไปยังทิศทางของ target } } else // Player die :( แงงงง ตายเลยยยย TT^TT { stopWalking(); // สั่งให้หยุดเดิน } } } else // Monster Die { anim.SetBool(dieHash, true); } }
public void followContinue() { following = true; onMonsterReachHandler -= followContinue; }