void Start() { nav2DAgent = GetComponent <Nav2DAgent>(); character = GetComponent <Character>(); nav2DAgent.Speed = character.GetSpeed(); nav2DAgent.addSeparationIgnored(GameObject.FindWithTag("Player")); characterName = FantasyNameGenerator.GetCharacterNameGenerator(new FantasyNameSettings( RPGKit.FantasyNameGenerator.Generators.Classes.Warrior, RPGKit.FantasyNameGenerator.Generators.Race.None, false, UnityEngine.Random.value > 0.5 ? RPGKit.FantasyNameGenerator.Generators.Gender.Male : RPGKit.FantasyNameGenerator.Generators.Gender.Female) ).GetFantasyName().FullName; }
//slowing when there is an obstacle ahead. void LookAhead() { if (lookAheadDistance <= 0) { return; } float currentLookAheadDistance = Mathf.Lerp(0, lookAheadDistance, velocity.magnitude / maxSpeed); Vector3 lookAheadPos = position + velocity.normalized * currentLookAheadDistance; Debug.DrawLine(position, lookAheadPos, Color.blue); if (!polyNav.PointIsValid(lookAheadPos)) { velocity -= (lookAheadPos - position); } if (avoidRadius > 0) { for (int i = 0; i < allAgents.Count; i++) { Nav2DAgent otherAgent = allAgents[i]; if (otherAgent == this || otherAgent.avoidRadius <= 0) { continue; } float mlt = otherAgent.avoidRadius + this.avoidRadius; float dist = (lookAheadPos - otherAgent.position).magnitude; Vector3 str = (lookAheadPos - otherAgent.position).normalized * mlt; Vector3 steer = Vector3.Lerp((Vector3)str, Vector3.zero, dist / mlt); velocity += new Vector3(steer.x, steer.y, 0); Debug.DrawLine(otherAgent.position, otherAgent.position + str, new Color(1, 0, 0, 0.1f)); } } }
// Start is called before the first frame update void Start() { navAgent = GetComponent <Nav2DAgent>(); nonPlayerCharacter = GetComponent <NonPlayerCharacter>(); stateMachine = GetComponent <StateMachine>(); }
override public void StateUpdate(StateMachine stateMachine, ActiveLinking activeLinking) { NonPlayerCharacter npc = stateMachine.GetComponent <NonPlayerCharacter>(); Character character = stateMachine.GetComponent <Character>(); Nav2DAgent navAgent = stateMachine.GetComponent <Nav2DAgent>(); character.currentSpeed = CharacterSpeed.SLOW; if (!npc.nav2DAgent.isMoving && !npc.nav2DAgent.isWaitingForPath) { //Debug.Log("moving is false"); var colliders = Physics2D.OverlapCircleAll(stateMachine.transform.position, findClosestBuildingRadius, LayerMask.GetMask(buildingFindLayers)); if (colliders.Count() <= 0) { return; } var buildingQueue = activeLinking.GetValueOrDefault <Queue <Queue <Vector2> > >("buildings"); var cornerQueue = activeLinking.GetValueOrDefault <Queue <Vector2> >("corners"); if (cornerQueue.Count < 1) { if (buildingQueue.Count < 1) { var closestColliders = colliders.OrderBy( x => (x.transform.position - stateMachine.transform.position).magnitude ); foreach (var collider in closestColliders) { var c = collider.GetComponentInParent <BuildingController>(); // if has building component and parent building is on building layer (walls are building that cannot be patrolled) if (c == null || c.gameObject.layer != LayerMask.NameToLayer("Building")) { var l = LayerMask.GetMask("Building"); continue; } buildingQueue.Enqueue(new Queue <Vector2>(c.WorldSpaceCorners)); } } if (buildingQueue.Count < 1) { return; } var corners = new List <Vector2>(buildingQueue.Dequeue()) .Where(x => { // only take corners that are not inside a active spell var c = Physics2D.OverlapPointAll(x, LayerMask.GetMask(spellTargetLayers)); foreach (var collider in c) { var target = collider.GetComponent <MagicTargetBase>(); if (!target.spellDone) { return(false); } } // and the corner is accessable; return(navAgent.navGrid.CanBeReached(x)); }).ToList(); // patrolling around building can be clockwise or anticlockwise if (Random.value > 0.5) { corners.Reverse(); } var closesCornerIndex = corners.FindIndex(x => x == corners.MinBy(c => (c - (Vector2)stateMachine.transform.position).magnitude).First() ); // shift the values so that the closest corner is first for (var i = 0; i < corners.Count; i++) { cornerQueue.Enqueue(corners[(closesCornerIndex + i) % corners.Count]); } } else { npc.MoveToOrFollow( cornerQueue.Dequeue() ); } } }
// Start is called before the first frame update void Start() { navAgent = GetComponent <Nav2DAgent>(); character = GetComponent <Character>(); }