public void OnDrawGizmosSelected() { if (AutoWayPoint.waypoints.get_length() == 0) { this.RebuildWaypointList(); } IEnumerator enumerator = UnityRuntimeServices.GetEnumerator(this.connected); while (enumerator.MoveNext()) { AutoWayPoint autoWayPoint = (AutoWayPoint)RuntimeServices.Coerce(enumerator.Current, typeof(AutoWayPoint)); if (Physics.Linecast(this.get_transform().get_position(), autoWayPoint.get_transform().get_position())) { Gizmos.set_color(Color.get_red()); Gizmos.DrawLine(this.get_transform().get_position(), autoWayPoint.get_transform().get_position()); UnityRuntimeServices.Update(enumerator, autoWayPoint); } else { Gizmos.set_color(Color.get_green()); Gizmos.DrawLine(this.get_transform().get_position(), autoWayPoint.get_transform().get_position()); UnityRuntimeServices.Update(enumerator, autoWayPoint); } } }
//功能:Patrol IEnumerator Patrol() { var curWayPoint = AutoWayPoint.FindClosest(transform.position); while (true) { var waypointPosition = curWayPoint.transform.position; // Are we close to a waypoint? -> pick the next one! if (Vector3.Distance(waypointPosition, transform.position) < pickNextWaypointDistance) { curWayPoint = PickNextWaypoint(curWayPoint); } // Attack the player and wait until // - player is killed // - player is out of sight if (CanSeeTarget()) { yield return(StartCoroutine(AttackPlayer())); } // Move towards our target MoveTowards(waypointPosition); yield return(null); } }
void OnEnable() { myTransform = transform; Mathf.Clamp01(randomSpawnChance); // Activate the npc based on randomSpawnChance if (Random.value > randomSpawnChance) { Destroy(myTransform.gameObject); } else { //if there is no objectWithAnims defined, use the Animation Component attached to this game object if (objectWithAnims == null) { objectWithAnims = transform; } // Set all animations to loop objectWithAnims.GetComponent <Animation>().wrapMode = WrapMode.Loop; // Except our action animations, Dont loop those objectWithAnims.GetComponent <Animation>()["shoot"].wrapMode = WrapMode.Once; // Put idle and run in a lower layer. They will only animate if our action animations are not playing objectWithAnims.GetComponent <Animation>()["idle"].layer = -1; objectWithAnims.GetComponent <Animation>()["walk"].layer = -1; objectWithAnims.GetComponent <Animation>()["run"].layer = -1; objectWithAnims.GetComponent <Animation>()["walk"].speed = walkAnimSpeed; objectWithAnims.GetComponent <Animation>()["run"].speed = runAnimSpeed; objectWithAnims.GetComponent <Animation>().Stop(); //initialize NPCAI vars playerObj = Camera.main.transform.GetComponent <CameraKick>().playerObj; attackRangeAmt = attackRange; objectWithAnims.GetComponent <Animation>().CrossFade("idle", 0.3f); // Auto setup player as target through tags if (target == null && GameObject.FindWithTag("Player") && targetPlayer) { target = GameObject.FindWithTag("Player").transform; } if (doPatrol) { curWayPoint = firstWayPoint; StartCoroutine(Patrol()); } else { StartCoroutine(StandWatch()); } if (!targetPlayer) { //ignore collisions with player if NPC is not targeting player to prevent physics oddities myTransform.gameObject.layer = 9; } } }
public void Into() { RaycastHit raycastHit = default(RaycastHit); if (!Physics.Linecast(this.get_transform().get_position(), this.owner.get_transform().get_position(), ref raycastHit)) { this.currWayPoint = null; } }
IEnumerator Patrol() { CharacterController controller = GetComponent <CharacterController>(); if (curWayPoint) //patrol if NPC has a current waypoint, otherwise stand watch { while (true) { Vector3 waypointPosition = curWayPoint.transform.position; // Are we close to a waypoint? -> pick the next one! if (Vector3.Distance(waypointPosition, myTransform.position) < pickNextWaypointDistance) { curWayPoint = PickNextWaypoint(curWayPoint); } //if NPC spawns in the air, move their character controller to the ground if (!controller.isGrounded) { Vector3 down = myTransform.TransformDirection(-Vector3.up); controller.SimpleMove(down); } else { //determine if player is within sight of NPC if (target && lastSearchTime + 0.75f < Time.time) { lastSearchTime = Time.time; if (CanSeeTarget()) { yield return(StartCoroutine(AttackPlayer())); } } } //determine if NPC should walk or run on patrol if (walkOnPatrol) { speedAmt = 1.0f; } else { speedAmt = speed; } // Move towards our target MoveTowards(waypointPosition); yield return(new WaitForFixedUpdate()); } } else { StartCoroutine(StandWatch()); yield break; } }
public void RebuildWaypointList() { object[] array = Object.FindObjectsOfType(typeof(AutoWayPoint)); AutoWayPoint.waypoints = new Array(array); IEnumerator enumerator = UnityRuntimeServices.GetEnumerator(AutoWayPoint.waypoints); while (enumerator.MoveNext()) { AutoWayPoint autoWayPoint = (AutoWayPoint)RuntimeServices.Coerce(enumerator.Current, typeof(AutoWayPoint)); autoWayPoint.RecalculateConnectedWaypoints(); UnityRuntimeServices.Update(enumerator, autoWayPoint); } }
//pick the next waypoint and determine if patrol //should continue forward or backward through waypoint group AutoWayPoint PickNextWaypoint(AutoWayPoint currentWaypoint) { AutoWayPoint best = currentWaypoint; foreach (AutoWayPoint cur in currentWaypoint.connected) { if (!countBackwards) { if (currentWaypoint.waypointNumber != cur.connected.Count) { if (currentWaypoint.waypointNumber + 1 == cur.waypointNumber) { best = cur; break; } } else if (currentWaypoint.waypointNumber == cur.connected.Count) { if (currentWaypoint.waypointNumber - 1 == cur.waypointNumber) { best = cur; countBackwards = true; break; } } } else { if (currentWaypoint.waypointNumber != 1) { if (currentWaypoint.waypointNumber - 1 == cur.waypointNumber) { best = cur; break; } } else if (currentWaypoint.waypointNumber == 1) { if (currentWaypoint.waypointNumber + 1 == cur.waypointNumber) { best = cur; countBackwards = false; break; } } } } return(best); }
public static AutoWayPoint FindClosest(Vector3 pos) { // The closer two vectors, the larger the dot product will be. AutoWayPoint closest = new AutoWayPoint(); float closestDistance = 1000.0f; foreach (AutoWayPoint cur in waypoints) { float distance = Vector3.Distance(cur.transform.position, pos); if (distance < closestDistance) { closestDistance = distance; closest = cur; } } return closest; }
public void RecalculateConnectedWaypoints() { this.connected = new Array(); IEnumerator enumerator = UnityRuntimeServices.GetEnumerator(AutoWayPoint.waypoints); while (enumerator.MoveNext()) { AutoWayPoint autoWayPoint = (AutoWayPoint)RuntimeServices.Coerce(enumerator.Current, typeof(AutoWayPoint)); if (!(autoWayPoint == this)) { if (!Physics.CheckCapsule(this.get_transform().get_position(), autoWayPoint.get_transform().get_position(), AutoWayPoint.kLineOfSightCapsuleRadius)) { this.connected.Add(autoWayPoint); UnityRuntimeServices.Update(enumerator, autoWayPoint); } } } }
static public AutoWayPoint FindClosest(Vector3 pos) { // The closer two vectors, the larger the dot product will be. AutoWayPoint closest = null; float closestDistance = 100000.0f; foreach (var cur in waypoints) { var distance = Vector3.Distance(cur.transform.position, pos); if (distance < closestDistance) { closestDistance = distance; closest = cur; } } return(closest); }
public static AutoWayPoint FindClosest(Vector3 pos) { AutoWayPoint result = null; float num = 100000f; IEnumerator enumerator = UnityRuntimeServices.GetEnumerator(AutoWayPoint.waypoints); while (enumerator.MoveNext()) { AutoWayPoint autoWayPoint = (AutoWayPoint)RuntimeServices.Coerce(enumerator.Current, typeof(AutoWayPoint)); float num2 = Vector3.Distance(autoWayPoint.get_transform().get_position(), pos); UnityRuntimeServices.Update(enumerator, autoWayPoint); if (num2 < num) { num = num2; result = autoWayPoint; UnityRuntimeServices.Update(enumerator, autoWayPoint); } } return(result); }
public AutoWayPoint FindClosest(Vector3 pos, int waypointsToFollow) { // The closer two vectors, the larger the dot product will be. AutoWayPoint closest = null; float closestDistance = 75.0f; foreach (AutoWayPoint cur in connected) { float distance = Vector3.Distance(cur.transform.position, pos); if (distance < closestDistance) { //track this waypoint only if it is in our waypoint group if (waypointsToFollow == cur.GetComponent <AutoWayPoint>().waypointGroup) { closestDistance = distance; closest = cur; } } } return(closest); }
public AutoWayPoint PickNextWaypoint(AutoWayPoint currentWaypoint) { Vector3 vector = this.get_transform().TransformDirection(Vector3.get_forward()); AutoWayPoint result = currentWaypoint; float num = 10f * (float)-1; IEnumerator enumerator = UnityRuntimeServices.GetEnumerator(currentWaypoint.connected); while (enumerator.MoveNext()) { AutoWayPoint autoWayPoint = (AutoWayPoint)RuntimeServices.Coerce(enumerator.Current, typeof(AutoWayPoint)); Vector3 vector2 = Vector3.Normalize(autoWayPoint.get_transform().get_position() - this.get_transform().get_position()); UnityRuntimeServices.Update(enumerator, autoWayPoint); float num2 = Vector3.Dot(vector2, vector); if (num2 > num && autoWayPoint != currentWaypoint) { num = num2; result = autoWayPoint; UnityRuntimeServices.Update(enumerator, autoWayPoint); } } return(result); }
AutoWayPoint PickNextWaypoint(AutoWayPoint currentWaypoint) { // We want to find the waypoint where the character has to turn the least // The direction in which we are walking var forward = transform.TransformDirection(Vector3.forward); // The closer two vectors, the larger the dot product will be. var best = currentWaypoint; var bestDot = -10.0; foreach (var cur in currentWaypoint.connected) { var direction = Vector3.Normalize(cur.transform.position - transform.position); var dot = Vector3.Dot(direction, forward); if (dot > bestDot && cur != currentWaypoint) { bestDot = dot; best = cur; } } return(best); }
AutoWayPoint PickNextWaypoint(AutoWayPoint currentWaypoint) { // We want to find the waypoint where the character has to turn the least // The direction in which we are walking Vector3 forward = transform.TransformDirection(Vector3.forward); // The closer two vectors, the larger the dot product will be. AutoWayPoint best = currentWaypoint; float bestDot = -10.0f; foreach (AutoWayPoint cur in currentWaypoint.connected) { Vector3 direction = Vector3.Normalize(cur.transform.position - transform.position); float dot = Vector3.Dot(direction, forward); if (dot > bestDot && cur != currentWaypoint) { bestDot = dot; best = cur; } } return best; }