// Update is called once per frame void Update() { if (unitState != e_unitStates.moving && enemiesInRange.Count > 0) { if (enemiesInRange[0] != null) { print("attacking: " + enemiesInRange[0].name); timer -= Time.deltaTime; if (timer <= 0.0f) { Attack(enemiesInRange[0].GetComponent <AgentMovement>()); } //attack(enemiesInRange[0]); } else { enemiesInRange.RemoveAt(0); } } else if (agent.remainingDistance > destinationThreshold && (unitState == e_unitStates.moving || enemiesInRange.Count == 0)) { agent.Resume(); } if (unitState != e_unitStates.stationary && agent.remainingDistance <= destinationThreshold) { if (agent.remainingDistance == 0) { agent.Stop(); unitState = e_unitStates.stationary; } else if (targetOccupied) { agent.Stop(); unitState = e_unitStates.stationary; } } if (Input.GetMouseButtonDown(1)) { if (Input.GetKey(KeyCode.LeftControl)) { RaycastHit hit; if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100)) { moveCommand(hit.point, null, true); } } else { RaycastHit hit; if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100)) { moveCommand(hit.point); } } } }
// Use this for initialization void Start() { agent = GetComponent <NavMeshAgent>(); unitState = e_unitStates.stationary; enemiesInRange = new List <GameObject>(); agent.speed = speed; }
void moveCommand(Vector3 targetPos, GameObject targetEnemy = null, bool attackMove = false) { target.position = targetPos; enemyTarget = targetEnemy; //set this unit's state if (!attackMove) { unitState = e_unitStates.moving; } else { unitState = e_unitStates.attackmoving; } agent.SetDestination(targetPos); }