c# - Trying to launch a projectile towards a gameobject, doesn't move!= -


i'm making 2d tower defense game , want towers launch prefab @ minions. spawns desired prefab, doesn't move it.

my 2 scripts:

public class attacker : monobehaviour {  // public variables public gameobject ammoprefab; public float reloadtime; public float projectilespeed;  // private variables private transform target;   // use initialization void start () { }  // update called once per frame void update () {  } void ontriggerenter(collider co){     if (co.gameobject.tag == "enemy" || co.gameobject.tag == "blocktower") {          debug.log("enemy tag detected");          if(this.gameobject.tag == "enemy" && co.gameobject.tag != "enemy"){             debug.log("this enemy");             // insert enemey attack block towers.         }         if(this.gameobject.tag == "tower" && co.gameobject.tag != "blocktower"){             debug.log("this tower");             tower tower = getcomponent<tower>();             tower.calculatecombattime(reloadtime, projectilespeed);             transform sendthis = co.transform;             tower.settarget(sendthis);         }     } } 

}

and

public class tower : monobehaviour { private transform target; private float firespeed; private double nextfiretime; private gameobject bullet; private attacker source;  // use initialization public virtual void start () {     source = this.getcomponent<attacker> (); }  // update called once per frame public virtual void update () {      if (target) {         debug.log("i have target");         //if(nextfiretime <= time.deltatime)         fireprojectile ();     } } public void calculatecombattime(float time, float speed){     debug.log("calculate combat speed");     nextfiretime = time.time + (time * .5);     firespeed = speed; } public void settarget(transform position){     debug.log("set target");     target = position; } public void fireprojectile(){     debug.log("shoot projectile");     bullet = (gameobject)instantiate (source.ammoprefab, transform.position, source.ammoprefab.transform.rotation);     float speed = firespeed * time.deltatime;     bullet.transform.position = vector3.movetowards (bullet.transform.position, target.position, speed); } 

}

basicly attacker detects object collides it, if tag tower send information tower. debug shows every function works, "debug.log("shoot projectile");" shows up.

however doesn't move towards target guess "bullet.transform.position = vector3.movetowards (bullet.transform.position, target.position, step);" never being executed?

vector3.movetowards moves object once, it's instant displacement when fireprojectile called.

you need create kind of projectile script update() function make move on time.

here example:

public class projectile : monobehaviour {     public vector3 targetposition;      void update()     {         transform.position = vector3.movetowards(transform.position, targetposition, speed * time.deltatime);     } } 

then right after bullet instantiation, set target:

bullet.getcomponent<projectile>().targetposition = target.position; 

hope helps.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -