public void Init(GameObject npc, int id) { targetNPC = npc; this.id = id; initialized = true; agent = GetComponent <NavMeshAgent>(); interaction = GetComponent <ObjectInteraction>(); anim = GetComponent <IiroAnimBehavior>(); interaction.setTarget(targetNPC); if (id == 0) { trolley = transform.FindChild("Trolley"); } }
// Update is called once per frame void Update() { if (initialized && !allDone) { /* Behavior for trolley nurse */ if (id == 0) { if (dest == Vector3.zero) { agent.stoppingDistance = 200.0f; dest = targetNPC.transform.position; interaction.setTarget(targetNPC); agent.SetDestination(dest); } else if (arrivedToDestination(250.0f) && !readyToLeave) { if (agent.stoppingDistance > 100.0f) { agent.stoppingDistance = 90.0f; } if (arrivedToDestination(100.0f)) { agent.Stop(); readyForLift = true; if (partner.readyToLeave) { targetNPC.GetComponent <NavMeshAgent>().enabled = false; targetNPC.transform.position = new Vector3(trolley.position.x, 24.0f, trolley.position.z); targetNPC.transform.rotation = Quaternion.Euler(new Vector3(trolley.eulerAngles.x, trolley.eulerAngles.y + 90.0f, trolley.eulerAngles.z)); targetNPC.transform.SetParent(trolley); targetNPC.transform.localPosition = new Vector3(targetNPC.transform.localPosition.x - 20f, targetNPC.transform.localPosition.y, targetNPC.transform.localPosition.z); readyToLeave = true; NavMeshHit hit; dest = startPos; NavMesh.SamplePosition(dest, out hit, 50.0f, NavMesh.AllAreas); agent.stoppingDistance = 200.0f; dest = hit.position; moveToDest(); agent.Resume(); } } } else if (partner.readyForLift && !arrivedToDestination(100.0f) && !readyToLeave) { timer += Time.deltaTime; if (timer > 5.0f) { timer = 0; agent.Warp(agent.destination); } } } /* Behavior for picker nurse */ else if (id == 1) { if (dest == Vector3.zero) { agent.stoppingDistance = 150.0f; interaction.setTarget(targetNPC); dest = interaction.getDestToTargetNPCSide(1, 16.0f); moveToDest(); } else if (arrivedToDestination(200.0f)) { agent.stoppingDistance = 50.0f; if (!readyForLift) { readyForLift = true; } if (!anim.pickingup && !readyToLeave && partner.readyForLift) { if (interaction.RotateTowards(targetNPC.transform)) { anim.pickfromfloor(); } } else if (anim.pickingup) { timer += Time.deltaTime; } if (timer > 2.0f) { readyToLeave = true; timer = 0; anim.StopAll(); dest = startPos; moveToDest(); } } } /* if npc is picked up and arrived to ER */ if (readyToLeave && arrivedToDestination(200.0f)) { /* Lower the stoppingDistance to make nurse with trolley more accurate with stopping */ agent.stoppingDistance = 50.0f; if (readyToLeave && arrivedToDestination(100.0f)) { allDone = true; } } /*If stuck and partner already left, mark as allDone*/ else if (readyToLeave && partner == null) { timer += Time.deltaTime; if (timer > 5.0f) { allDone = true; } } } }
void handleInput() { if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0))) { if (!isMouseOverUI()) { if (tooltip.enabled) { tooltip.Deactivate(); } } RaycastHit hit2; //Layer masks LayerMask layerMask = (1 << 8) | (1 << 11); LayerMask layerMaskNpc = (1 << 9) | (1 << 10); Ray ray = new Ray(); //for unity editor #if UNITY_EDITOR || UNITY_STANDALONE ray = Camera.main.ScreenPointToRay(Input.mousePosition); //for touch device #elif (UNITY_ANDROID || UNITY_IPHONE || UNITY_WP8) ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position); #endif /* * Prioritize raycast hit on npc, so that when npc is sitting * or sleeping you hit the npc instead of the object they are on */ RaycastHit[] rays; rays = Physics.RaycastAll(ray, 10000.0f, layerMaskNpc); List <RaycastHit> hits = new List <RaycastHit>(); bool npcwashit = false; movingToTarget = false; if (rays.Length > 0) { if (!isMouseOverUI()) { anim.StopAll(); pickingup = false; foreach (RaycastHit hit in rays) { //object who booked the object. GameObject temp = objManager.isObjectBooked(hit.transform.gameObject); if (hit.transform.tag != "NPC" && temp == null) { hits.Add(hit); continue; } else { npcwashit = true; if ((target == hit.transform.gameObject) || (temp != null && target == temp)) { nextDestination = new Vector3(target.transform.position.x, target.transform.position.y, target.transform.position.z); moveTo(nextDestination); followNpc = true; } else { disableTarget(); if (temp != null && temp != gameObject) { target = temp; } else { target = hit.transform.gameObject; } interaction.setTarget(target); outlineGameObjectRecursive(target.transform, Shader.Find("Outlined/Silhouetted Diffuse")); } if (sitting) { sitting = false; objManager.unbookObject(interaction.getCurrentChair()); } if (sleeping) { sleeping = false; objManager.unbookObject(interaction.getBed()); } } } //if ther was now npc's hit, take the first hit object if (!npcwashit) { hit2 = hits[0]; if (target == hit2.transform.gameObject) { if (sitting) { sitting = false; objManager.unbookObject(interaction.getCurrentChair()); } if (sleeping) { sleeping = false; objManager.unbookObject(interaction.getBed()); } if (target.tag == "Chair" || target.tag == "QueueChair" || target.tag == "Chair2") { if (objManager.bookTargetObject(target, gameObject)) { interaction.setCurrentChair(interaction.getTarget()); if (target.tag == "Chair2") { nextDestination = interaction.getDestToTargetObjectSide(1, 16.0f); moveTo(nextDestination); } else { nextDestination = interaction.getDestToTargetObjectSide(0, 16.0f); moveTo(nextDestination); } sitting = true; disableMoveIndicator(); } } else if (target.tag == "Bed") { if (objManager.bookTargetObject(target, gameObject)) { interaction.setBookedBed(interaction.getTarget()); nextDestination = interaction.getDestToTargetObjectSide(1, 16.0f); moveTo(nextDestination); sleeping = true; disableMoveIndicator(); } } else if (target.tag == "PickupItemFloor" || target.tag == "PickupItem") { interaction.setTarget(target); pickingup = true; nextDestination = target.transform.position; moveTo(nextDestination); disableMoveIndicator(); } else if (target.tag == "MedCabinet" || target.tag == "Computer" || target.tag == "TrashCan" || target.tag == "CoffeeMachine") { interaction.setTarget(target); nextDestination = target.transform.position; moveTo(nextDestination); disableMoveIndicator(); movingToTarget = true; } } else { if (sitting || interaction.getCurrentChair() != null) { sitting = false; objManager.unbookObject(interaction.getCurrentChair()); } if (sleeping || interaction.getBed() != null) { sleeping = false; objManager.unbookObject(interaction.getBed()); } /*Disable target*/ disableTarget(); GameObject temp = objManager.isObjectBooked(hit2.transform.gameObject); if (temp != null) { target = temp; } else { //set new target target = hit2.transform.gameObject; } interaction.setTarget(target); //outline the object outlineOnlyParent(target.transform, Shader.Find("Outlined/Silhouetted Diffuse")); } } } } //check if the ray hits floor collider else if (Physics.Raycast(ray, out hit2, 10000.0f, layerMask)) { if (!isMouseOverUI()) { anim.StopAll(); pickingup = false; if (interaction.getCurrentChair() != null) { objManager.unbookObject(interaction.getCurrentChair()); } if (interaction.getBed() != null) { objManager.unbookObject(interaction.getBed()); } //get position of hit and move there Vector3 pos = new Vector3(hit2.point.x, 0, hit2.point.z); enableMoveIndicator(pos); nextDestination = pos; moveTo(nextDestination); if (sitting == true) { sitting = false; objManager.unbookObject(target); } if (sleeping == true) { sleeping = false; objManager.unbookObject(target); } if (followNpc) { followNpc = false; } } } //check if the ray hits targetableobjects collider } }