public void STATE_Update(CivillianController agent, StateMachine_CIV stateMachine, float deltaTime) { if (agent.enableWander == true) { //Just set a random destination if (agent.navAgent.hasPath == false && agent.navAgent.enabled == true) { //Debug.Log("PickNewWander() called from " + agent.name); if (!agent.navAgent.SetDestination(PickNewWanderPoint())) { Debug.Log("SetDest failed: " + agent.name); } //agent.currentDest = agent.navAgent.destination; } //if (agent.navAgent.isPathStale == true) // Debug.Log(agent.name + " is stale"); //if (agent.navAgent.remainingDistance > agent.navAgent.stoppingDistance) // agent.Move(agent.navAgent.desiredVelocity, false, false); //else // agent.Move(Vector3.zero, false, false); } }
public void STATE_Update(CivillianController agent, StateMachine_CIV stateMachine, float deltaTime) { if (!stopped) { currentAgent.navAgent.SetDestination(itemPos); } float stoppingdist = 2.0f; if (stoppingdist >= Vector3.Distance(currentAgent.transform.position, currentAgent.target.transform.position) && !stopped) { stopped = true; currentAgent.m_Animator.SetBool("idle", true); currentAgent.navAgent.isStopped = true; //Create nav obstacle if (currentAgent.GetComponent <NavMeshObstacle>() == null) { NavMeshObstacle obstacle = currentAgent.gameObject.AddComponent <NavMeshObstacle>(); obstacle.shape = NavMeshObstacleShape.Capsule; obstacle.radius = 0.3f; obstacle.center = new Vector3(0, 1, 0); obstacle.carving = true; } else //Obstacle has already been added first time around so enable from here on out { currentAgent.GetComponent <NavMeshObstacle>().enabled = true; } currentAgent.navAgent.enabled = false; } }
public void STATE_Update(CivillianController agent, StateMachine_CIV stateMachine, float deltaTime) { //Check if we reached the scare threshold other wise continue with normal retreat state RunScaredExit(); if (agent.hasDroppedEcto == false) { agent.hasDroppedEcto = true; Vector3 dropPos = new Vector3(currentAgent.transform.position.x, currentAgent.transform.position.y + 1f, currentAgent.transform.position.z); GameObject.Instantiate(ectoplasm, dropPos, currentAgent.transform.rotation); } //This is the code that makes the agent run away from whatever target has been set if (scared == false) { Debug.Log("Scared"); //Get the direction away from the target object that they are trying to flee from if (currentAgent.TRIGGERED_hit) { dirAwayFromObject = currentAgent.transform.position - currentAgent.collidedItemPos; } else { dirAwayFromObject = currentAgent.transform.position - currentAgent.target.transform.position; } //Just minus a default value for now from the scared score //Overtime minus 1 from the scared value - possibly change in the future if (currentAgent.currentScareValue <= 0) { currentAgent.currentScareValue = 0; } else { scareDecreaseTimer -= Time.deltaTime; if (scareDecreaseTimer <= 0f) { currentAgent.currentScareValue = currentAgent.currentScareValue - 1; scareDecreaseTimer = 3.0f; } } //Update the debug text currentAgent.txtScaredValue.text = currentAgent.currentScareValue.ToString(); //If will is not close to the agent then don't flee if (dirAwayFromObject.magnitude > scaredRadius) { currentAgent.navAgent.velocity = Vector3.zero; //Stop the agent from moving currentAgent.m_Animator.SetBool("idle", true); //Debug.Log(timer); timer -= Time.deltaTime; //minus the time if (timer <= 0f) { currentAgent.m_Animator.SetBool("idle", false); stateMachine.ChangeState(agent, new CIV_Wander()); //Change back to wander timer = 3.0f; //Reset timer } } else //The player has moved close to the agent so move the agent { timer = 3.0f; //Reset the timer currentAgent.m_Animator.SetBool("idle", false); if (agent.navAgent.hasPath == false) { agent.navAgent.SetDestination(RandomNavSphere(currentAgent.transform.position, 10, -1)); } } //End else } //End Scared if //Hack to turn back on the agent because we lost the reference of the otherAgent because they left the scene if (currentAgent.isStationary) { Debug.Log("stationay"); currentAgent.GetComponent <NavMeshObstacle>().enabled = true; currentAgent.m_Animator.SetBool("idle", false); currentAgent.navAgent.enabled = true; currentAgent.isStationary = false; } } //End update
public Color civilianTop2Colour = Color.black; // 31/01/2018 Added by Mark - For custom colours // Use this for initialization void Start() { //Debug.Log("GetInstanceID: " + GetInstanceID()); id = GetInstanceID(); //Debug.Log("GetID: " + GetID()); //Temporary possibly - shouldnt need to setup an enum for currentState when i already have a way to detect it in statemachine but cant actually seem to get that working... currentState = State.State_Wander; lineOfSight = Camera.main.GetComponent <valueController>().civillianLineOfSight; hasDroppedEcto = false; currentScareValue = 0; navAgent = gameObject.GetComponent <NavMeshAgent>(); m_Animator = GetComponent <Animator>(); //m_Rigidbody = GetComponent<Rigidbody>(); //Find EndPoint endPoint = GameObject.FindWithTag("EndPoint").transform; //Assign a random priority to this agent, hopefully fixing the random npcs getting caught on eachother //navAgent.avoidancePriority = Random.Range(1, 99); //Increment the NPC count GameManager.Instance.NPCcount++; //If the text canvas is not in this position - will throw an error txtState = transform.GetChild(0).GetChild(0).GetComponent <Text>(); //Accesses the txtState Text object in the heirachy attached to this Agent txtScaredValue = transform.GetChild(0).GetChild(1).GetComponent <Text>(); //Show the debug states in editor #if UNITY_EDITOR { txtState.enabled = true; txtScaredValue.enabled = true; } #else { txtState.enabled = false; txtScaredValue.enabled = false; } #endif //Set target to run away from target = GameManager.Instance.player.gameObject; sid = target.gameObject; civIconStateScript = GetComponent <script_civilianIconState>(); // 19-12-2017 Added by Mark civIconStateScript.myState = script_civilianIconState.gameState.normal; // 19-12-2017 Added by Mark //Fmod instance creation FMOD_ScaredInstance = FMODUnity.RuntimeManager.CreateInstance(GameManager.Instance.audioCivScared); //StateMachine creation - Setting Default State - Will inherit this from inspector m_stateMachine = new StateMachine_CIV(); m_stateMachine.ChangeState(this, new CIV_Wander()); rend = rendererGeo.GetComponent <Renderer>(); // 31/01/2018 Added by Mark - For custom colours rend.material.SetColor("_PantsColour", civilianPantsColour); // 31/01/2018 Added by Mark - For custom colours rend.material.SetColor("_Top1Colour", civilianTop1Colour); // 31/01/2018 Added by Mark - For custom colours rend.material.SetColor("_Top2Colour", civilianTop2Colour); // 31/01/2018 Added by Mark - For custom colours //DEBUGGING name = "Civ " + id; }