public void Sense() { if (preferClosestTarget) { GameObject go = sight.GetClosest(transform.position); if (go != null) { target = go.transform; } } if (target == null || !sight.IsVisible(target.gameObject)) { if (sight.perceived.Count > 0) { if (preferClosestTarget) { GameObject go = sight.GetClosest(transform.position); if (go != null) { target = go.transform; } } else { GameObject go = sight.GetRandom(); if (go) { target = go.transform; } } } else { target = null; } } if (target) { currentPosition = target.position; direction = currentPosition - this.transform.position; distance = direction.magnitude; if (distance < aimRange) { lastKnownPosition = currentPosition; } } }
public void Sense() { if (player == null || !sensePlayer.IsVisible(player.gameObject)) { if (sensePlayer.perceived.Count > 0) { player = sensePlayer.GetRandom().transform; } else { player = null; } } if (player) { Vector3 currentPlayerPosition = player.position; playerDirection = currentPlayerPosition - this.transform.position; distance = playerDirection.magnitude; if (distance < seeDistance) { lastKnownPlayerPosition = currentPlayerPosition; } } if (friend == null || !senseFriends.IsVisible(friend.gameObject)) { if (senseFriends.perceived.Count > 0) { try { friend = senseFriends.GetClosest(this.transform.position).transform; } catch (System.Exception) { friend = null; } } else { friend = null; } } }
void Update() { // sens GameObject go = sight.GetClosest(transform.position); if (go) { target = go.transform; } else { target = null; } if (target) { Vector3 direction = target.position - this.transform.position; if (direction.magnitude < aimRange) { position = target.position + offset * Vector3.up; } } // act laser.enabled = true; if (target) { if (direction.magnitude < aimRange) { pivot.LookAt(position); if (direction.magnitude < shootRange) { weapon.Fire(); laser.enabled = false; } } } else { time += Time.deltaTime; lasertime += Time.deltaTime; if (time > nextTime) { time = 0; nextTime = Random.Range(0, idleTime) + 3; u = Random.Range(0, 360); v = Random.Range(0, 30); if (Random.Range(0, 1.0f) < 0.3f) { audioSource.PlayOneShot(clipAudio); } } if (lasertime > lasernextTime) { lasertime = 0; lasernextTime = Random.Range(0, idleTime / 4) + 0.3f; w = Random.Range(1.2f, 1.8f); } pivot.localRotation = Quaternion.RotateTowards(pivot.localRotation, Quaternion.Euler(new Vector3(-v, u, 0)), speed * Time.deltaTime); laser.SetPosition(1, Vector3.MoveTowards(laser.GetPosition(1), new Vector3(0, 0, w), Time.deltaTime)); } }