public void MoveStates() { switch (moveFSM) { case MoveFSM.empty: break; case MoveFSM.findPosition: { RemoveUnitFromUnitManagerMovingUnitsList(); PathRequestManager.RequestPath(new PathRequest(transform.position, target, OnPathFound)); moveFSM = MoveFSM.move; } break; case MoveFSM.recalculatePath: { Node targetNode = grid.NodeFromWorldPoint(target); if (targetNode.walkable == false) { stopMoving = false; FindClosestWalkableNode(targetNode); moveFSM = MoveFSM.move; } else if (targetNode.walkable == true) { stopMoving = false; PathRequestManager.RequestPath(new PathRequest(transform.position, target, OnPathFound)); moveFSM = MoveFSM.move; } } break; case MoveFSM.move: Move(); break; case MoveFSM.turnToFace: //TurnToFace(); break; case MoveFSM.interact: // if(currentInteractable != null) //currentInteractable.GetComponent<Interactable>().Interact(this.gameObject); break; case MoveFSM.moveToTarget: MoveToTarget(); break; } }
public void OnPathFound(Vector3[] newPath, bool pathSuccessful) { if (pathSuccessful) { path = newPath; targetIndex = 0; RemoveUnitFromUnitManagerMovingUnitsList(); UnitManager.instance.movingUnits.Add(this.gameObject); StopCoroutine("FollowPath"); StartCoroutine("FollowPath"); moveFSM = MoveFSM.move; } }
public void MoveToTarget() { if (transform.position != target) { transform.rotation = Quaternion.LookRotation(target - transform.position); transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime); } else if (transform.position == target) { animator.SetFloat(walkSpeedId, 0f); moveFSM = MoveFSM.move; } }
private void FindClosestWalkableNode(Node originalNode) { Node comparisonNode = grid.grid[0, 0]; Node incrementedNode = originalNode; for (int x = 0; x < incrementedNode.gridX; x++) { // Debug.Log("x: " + incrementedNode.gridX + " incremented node - 1: " + (incrementedNode.gridX - 1)); incrementedNode = grid.grid[incrementedNode.gridX - 1, incrementedNode.gridY]; if (incrementedNode.walkable == true) { comparisonNode = incrementedNode; target = comparisonNode.nodeWorldPosition; PathRequestManager.RequestPath(transform.position, target, OnPathFound); moveFSM = MoveFSM.move; break; } } }
public void Init() { //_player = FindObjectOfType<Player>(); MoveFSM moveState = gameObject.AddComponent <MoveFSM>(); IdleFSM idleState = gameObject.AddComponent <IdleFSM>(); moveState.Init(); idleState.Init(); if (_stateDic.ContainsKey(State.Idle) && _stateDic.ContainsKey(State.Move)) { _stateDic.Add(State.Idle, idleState); _stateDic.Add(State.Move, moveState); } SetState(State.Idle); CreateComponent(); _aim.Enter(); StartCoroutine(IEStateChecker()); }
void GetInteraction() { //for getting the point click store to this value Ray interactionRay = Camera.main.ScreenPointToRay(Input.mousePosition); // typical raycast stuff RaycastHit interactionInfo; if (Physics.Raycast(interactionRay, out interactionInfo, Mathf.Infinity)) { GameObject interactedObject = interactionInfo.collider.gameObject; // if clicked on a npc if (interactedObject.tag == "Interactable Object") { interactedObject.GetComponent <Interactable>().MoveToInteraction(playerAgent); } // here we will call player to stop when click a obstacle else if (interactedObject.tag == "Obstacle") { pathReached = true; // animator.SetBool("isPathReach", pathReached); animator.SetFloat("Speed", 0f); playerAgent.destination = playerAgent.transform.position; } else { // register the point then move the player playerAgent.stoppingDistance = 0f; playerAgent.destination = interactionInfo.point; pathReached = false; animator.SetTrigger("isRunning"); // animator.SetBool("isPathReach", pathReached); animator.SetFloat("Speed", 3f); moveFSM = MoveFSM.move; } } }
IEnumerator FollowPath() { currentWaypoint = path[0]; while (true) { if (transform.position == currentWaypoint) { targetIndex++; if (targetIndex >= path.Length) { // stopMoving = true; // currentWaypoint = new Vector3(999,999,999); moveFSM = MoveFSM.moveToTarget; yield break; } currentWaypoint = path[targetIndex]; } stopMoving = false; animator.SetFloat(walkSpeedId, 3f); transform.rotation = Quaternion.LookRotation(currentWaypoint - transform.position); transform.position = Vector3.MoveTowards(transform.position, currentWaypoint, speed * Time.deltaTime); yield return(null); } }