Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

GetComponent for a stored transform.

$
0
0
Hi, Im making a tower defense game but ran into some problems with my tower. When an enemy enters the tower's trigger, it stores their transform. For the tower's bullet that it instantiate, I want to use a GetComponent for the stored enemy transform, and insert it in the bullet's transform.LookAt() so that the bullet will go towards the enemy. However I cant seem to get the the GetComponet to properly access the stored transform within the tower. Sorry I forgot to add the scripts There are 3 scripts, the first one is attached to the actual tower, all it really does is instantiate the bullet from the attached game object // // // using UnityEngine; using System.Collections; public class TowerD : MonoBehaviour { //The seconds inbetween cannon fire public int Fire_Rate = 1; //The prefab used for the Cannon Ammo public GameObject Cannon_Ammo; //Where the Cannon Ammo comes out public Transform Cannon_Open; //The enemy target used for the trigger GameObject EnemyT; void Start() { StartCoroutine(FIRE()); } void Update() { } void OnTriggerEnter(Collider other) { if (other.tag == "Enemy") { EnemyT = other.gameObject; } } //Allows the Tower to fire then wait between each new fire IEnumerator FIRE() { Instantiate (Cannon_Ammo, Cannon_Open.position, Cannon_Open.rotation); yield return new WaitForSeconds(Fire_Rate); StartCoroutine(FIRE()); } } // // // This script is attached to an object that is attached to the tower. It has the main trigger so that when an enemy enters, it stores its transform, and adds it to its own transform.LookAt(); // // // using UnityEngine; using System.Collections; public class TargetingD : MonoBehaviour { //public List targets; public Transform target; //GetComponent for the Bullet class private Bullet bullet; void Start () { } void Update() { transform.LookAt(target); } //checks to see if an enemy has activated the trigger. If so, their transform is stored void OnTriggerEnter(Collider other) { //EnemyT = other.gameObject; if(other.tag == ("Enemy")) { target = other.transform; } } } This last script is on the bullet thats instantiated. Its in this script that tries to use the getComponent of the previous script, but doesnt seem to want to work. // // // using UnityEngine; using System.Collections; public class Bullet : MonoBehaviour { public float BulletLife = 1.0f; public float BulletSpeed = 1.0f; public Transform TargetTemp; public Transform StoreTarget; //GetComponent the TargetingD script private TargetingD TargetD; void Awake() { } void Start() { //TargetD = transform.Find } void Update() { //Back up if the get component wont work StoreTarget = TargetD.target; transform.LookAt(StoreTarget); //Target = StoreTarget.GetComponent(); //Cuases the bullet to move forward transform.Translate(Vector3.forward * BulletSpeed * Time.deltaTime, Space.Self); //Debug.Log(Target.EnemyT); } IEnumerator Life() { yield return new WaitForSeconds(BulletLife); Destroy(gameObject); } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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