private void OnTriggerEnter2D(Collider2D collision) { if (collision.name == "Heros") { activated = true; Player = collision.transform; NextNode = pathfinder.WorldPointFromNode(PathFindNodes(this.transform.position, Player.position)); PlayerNode = new Node(false, astargrid.NodeFromWorldPoint(Player.position).posX, astargrid.NodeFromWorldPoint(Player.position).posY); //Position du joueur au moment ou il rentre dans le trigger } }
// Update is called once per frame void Update() { if (!moving) { animator.SetBool("moving", false); return; } if (Time.time >= nextTime) { findPath = aStarPathfinding.FindPath(transform.position, player.transform.position); nextTime += Random.Range(0.5f, 1.5f); } // Nowhere to path-find. if (findPath.Count == 0) { return; } // Find the next cell to move into. var worldPoint = aStarPathfinding.WorldPointFromNode(findPath[0]); var target = worldPoint - transform.position; // If the next cell is too close and there are more cells, move towards the second-closest cell. if (target.magnitude < 1 && findPath.Count > 1) { worldPoint = aStarPathfinding.WorldPointFromNode(findPath[1]); target = worldPoint - transform.position; // Remove the now-too-close cell from the list. findPath.RemoveAt(0); } // Rotate towards next cell. transform.up = Vector3.Lerp(transform.up, target, 0.1f); // Move towards next cell. var movement = Vector3.ClampMagnitude(target, 1); animator.SetBool("moving", target.magnitude > 1); transform.position += movement * speed * Time.deltaTime; }
//---------------------캐릭터이동--------------------------// void Moving() { if (state == ActorState.move) { transform.position = Vector2.MoveTowards(transform.position, aStarPathfinding.WorldPointFromNode(pathNode[0]), stat.moveSpeed * Time.deltaTime); if (pathNode != null) { if (transform.position == aStarPathfinding.WorldPointFromNode(pathNode[0])) { pathNode.RemoveAt(0); } } currentTarget = null; /////////////////수정해야함 ... 이거때문에 이동할때 공격하지않음 ani.SetBool("walk", true); if (pathNode.Count == 0) { state = ActorState.idle; ani.SetBool("walk", false); curPosition = transform.position; } if (Vector2.Distance(transform.position, curPosition) < 2 && isCollidingWithPlayer) // 목적지 근처에서 Player끼리 Colliding 중이면 멈춤 { state = ActorState.idle; ani.SetBool("walk", false); curPosition = transform.position; } if (isCollidingWithEnemy || isCollidingWithPlayer) // 콜리더 충돌중이면 새로운 길 찾음 { //pathNode[0].isSolid = true; pathNode = aStarPathfinding.FindPath(transform.position, curPosition); } } if (state == ActorState.chase) { transform.position = Vector2.MoveTowards(transform.position, currentTarget.position, stat.moveSpeed * Time.deltaTime); ani.SetBool("walk", true); } }
void move() { //new position is closer to the first step on the rout to the player //so find this step Vector3 stepOne = pf.WorldPointFromNode(pf.FindPath(transform.position, player.transform.position)[0]); stepOne.z = 0; //dir to target Vector3 towardsPlayer = (stepOne - transform.position).normalized; rb.velocity = (towardsPlayer * speed); }