private void shoot() { _ammoRemaining[_selectedColor] -= 1; Vector3 direction = (Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position).normalized; GameObject b = (GameObject)(Instantiate(bullet, transform.position + direction * 1.2F, Quaternion.identity)); Bullet bulletScript = b.GetComponent(typeof(Bullet)) as Bullet; bulletScript.Color = _selectedColor; Rigidbody2D bulletBody = b.GetComponent <Rigidbody2D>(); bulletBody.velocity = direction * bulletSpeed; bulletScript.TargetTag = "Enemy"; source.PlayOneShot(shootSound, volHighRange); UpdateAmmoText(); //float vol = Random.Range (volLowRange, volHighRange); source.PlayOneShot(shootSound, volHighRange); // Remove ammo from ammo tail AmmoTail ammoTailScript = _lastTail.GetComponent(typeof(AmmoTail)) as AmmoTail; Destroy(_lastTail); _tailSize -= 1; _lastTail = ammoTailScript.isHead ? null : ammoTailScript.target; Destroy(b, 2); }
public void AddAmmo(Color color, int amount) { if (_ammoRemaining[color] >= 5) { return; } _ammoRemaining[color] += amount; for (int i = 0; i < amount; i++) { Ammo.Add(color); if (color == _selectedColor) { Rigidbody2D connectedBody; Vector3 spawnLocation; GameObject target; _tailSize += 1; // Create a new tail element and have it "follow" the previous end of the tail if (_lastTail == null) // No last tail, follow player instead { target = gameObject; spawnLocation = transform.position; } else { target = _lastTail; spawnLocation = _lastTail.transform.position; } GameObject ammoTailUnit = (GameObject)(Instantiate(ammotail, spawnLocation, Quaternion.identity)); AmmoTail ammoScript = ammoTailUnit.GetComponent(typeof(AmmoTail)) as AmmoTail; ammoScript.color = color; if (_lastTail != null) // Adding new tail element to existing tail { ammoScript.JumpThreshold = ammoScript.JumpThreshold / 2; } else { ammoScript.isHead = true; ammoScript.FollowDistance += 0.4F; } ammoScript.target = target; _lastTail = ammoTailUnit; } } UpdateAmmoText(); }
private void initializeAmmoTail() { // Destroy previous tail GameObject head = _lastTail; while (head != null && head != gameObject) { Destroy(head); AmmoTail ammoScript = head.GetComponent(typeof(AmmoTail)) as AmmoTail; head = ammoScript.target; } Color color = _selectedColor; int number = _ammoRemaining[color]; Vector3 spawnLocation = new Vector3(transform.position.x - 1, transform.position.y, transform.position.z); GameObject target = gameObject; bool first = true; for (int i = 0; i < number; i++) { GameObject ammoTailUnit = (GameObject)(Instantiate(ammotail, spawnLocation, Quaternion.identity)); AmmoTail ammoScript = ammoTailUnit.GetComponent(typeof(AmmoTail)) as AmmoTail; ammoScript.color = color; ammoScript.target = target; if (!first) { ammoScript.JumpThreshold = ammoScript.JumpThreshold / 2; } else { ammoScript.isHead = true; ammoScript.FollowDistance += 0.4F; } spawnLocation = new Vector3(spawnLocation.x - (i + 1), spawnLocation.y, spawnLocation.z); target = ammoTailUnit; _lastTail = ammoTailUnit; first = false; } }