Quantcast
Channel: Questions in topic: "tower defence"
Viewing all articles
Browse latest Browse all 10

Shooting bullet problem c#

$
0
0
The codes below showcase, a turret, bullet and enemy health script respectively. However i'm having some serious problems, currently the script instantiates 1000s of clones of prefab type bullet. Second i've tried using a collider to detect the enemies instead of a list, as i have little idea how to create a radius for one turret. The clones created all fall on top of another in the same position, and continue to do so even though the selected targets are moving forwards. using System.Collections; using System.Collections.Generic; public class TurretScript : MonoBehaviour { public List targets; public Transform selectedTarget; public Transform myTransform; public Transform objBullet; public Transform bulletSpawn; public BulletScript bulletScript; void Start(){ targets = new List(); AddAllEnemies(); selectedTarget = null; myTransform = transform; transform.Find ("bulletSpawn"); } public void AddAllEnemies(){ targets.Clear(); //this is here as enemies spawn and it needs to be reset each time GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy"); foreach(GameObject enemy in go) AddTarget(enemy.transform); } public void AddTarget(Transform enemy){ targets.Add(enemy); } private void TargetEnemy(){ if(selectedTarget == null) { SortTargetsByDistance(); selectedTarget = targets[0]; } else { int index = targets.IndexOf(selectedTarget); if(index < targets.Count - 1) { index++; } } } public void SortTargetsByDistance(){ targets.Sort(delegate(Transform t1, Transform t2){ return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position)); }); Debug.Log ("CodeWorking"); FireatEnemy(); } void Update(){ AddAllEnemies(); TargetEnemy(); transform.rotation = Quaternion.LookRotation(selectedTarget.transform.position-transform.position); } public void FireatEnemy(){ bulletScript.Fire(); } } using UnityEngine; using System.Collections; using System.Collections.Generic; public class BulletScript : MonoBehaviour { public GameObject Bullet; public float speed; public int Damage; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } public void Fire() { Instantiate(Bullet,transform.position,transform.rotation); Bullet.rigidbody.AddForce(transform.forward*speed); Debug.Log ("BulletFired"); DamageEnemy(); } void DamageEnemy() { EnemyScript e = gameObject.GetComponent(); e.TakeDamage(10); if (e.GetHealth() <=0) { Destroy (gameObject); } } } using UnityEngine; using System.Collections; public class EnemyScript : MonoBehaviour { public int Health; public float GetHealth() { return Health; } public void TakeDamage(int damage) { Health -= damage; } // Use this for initialization void Start () { } // Update is called once per frame void Update () { } }

Viewing all articles
Browse latest Browse all 10

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>