private void FixedUpdate() { if (!paused && currentAmmo > 0) { delayTimer -= Time.fixedDeltaTime; if (delayTimer <= 0) { delayTimer = delay + Random.value; currentAmmo--; //find a target Vector3 bestAttempt = track.GetRandomPoint(); for (int i = 0; i < attempts; i++) { Vector3 testPoint = track.GetRandomPoint(); if (Vector3.Distance(transform.position, testPoint) < Vector3.Distance(transform.position, bestAttempt)) { bestAttempt = testPoint; } } //spawn a cat GameObject cat = GameObject.Instantiate(catPrefab); CatBlock cBlock = cat.GetComponent <CatBlock>(); cBlock.target = bestAttempt; cBlock.transform.position = transform.position; } } }
private void OnCollisionStay(Collision collision) { CatBlock cat = collision.gameObject.GetComponent <CatBlock>(); if (cat != null) { cat.killTimer -= Time.fixedDeltaTime; if (cat.killTimer < 0) { Destroy(cat.gameObject); } } }
private void OnTriggerStay(Collider other) { CatBlock cat = other.gameObject.GetComponent <CatBlock>(); if (cat != null && cat.readyToTarget()) { if (nearestTarget == null) { nearestTarget = cat; } else { if (Vector3.Distance(transform.position, nearestTarget.transform.position) > Vector3.Distance(transform.position, cat.transform.position)) { nearestTarget = cat; } } } }
private void FixedUpdate() { cooldownTimer -= Time.fixedDeltaTime; cooldownTimer = Mathf.Max(0, cooldownTimer); if (cooldownTimer == 0 && nearestTarget != null) { //calculate our aim direction Vector3 targetPos = nearestTarget.transform.position; Vector3 targetVel = nearestTarget.GetComponent <Rigidbody>().velocity; Vector3 toTarget = targetPos - transform.position; float a = Vector3.Dot(targetVel, targetVel) - projectileVelocity * projectileVelocity; float b = 2 * Vector3.Dot(targetVel, toTarget); float c = Vector3.Dot(toTarget, toTarget); float p = -b / (2 * a); float q = Mathf.Sqrt((b * b) - 4 * a * c) / (2 * a); float t1 = p - q; float t2 = p + q; float t; if (t1 > t2 && t2 > 0) { t = t2; } else { t = t1; } Vector3 targetSpot = targetPos + targetVel * t; Vector3 aimDir = targetSpot - transform.position; aimDir.Normalize(); //fire the projectile! GameObject fireProjectile = GameObject.Instantiate(projectile); fireProjectile.GetComponent <Rigidbody>().velocity = aimDir * projectileVelocity; fireProjectile.transform.position = transform.position; //cleanup cooldownTimer = fireRate; nearestTarget = null; } }