c# - How to get a projectile to shoot towards a gameobject -
im working on turretai. have when enemy within range turret targets enemy i'm unable turret shoot projectiles toward enemy. have turret class.
using unityengine; using system.collections; public class defence : monobehaviour { public float distancefromcastle,cooldown; public gameobject enemy; public gameobject bullet; public int protectionradius,bulletspeed; // use initialization void start () { protectionradius = 35; bulletspeed = 50; cooldown = 5; } // update called once per frame void update () { enemy = gameobject.findgameobjectwithtag("enemy"); if(enemy != null) { distancefromcastle = vector3.distance(gameobject.findgameobjectwithtag("enemy").transform.position,gameobject.findgameobjectwithtag("defence").transform.position); //print (distancefromcastle); if(distancefromcastle <= protectionradius) { attackenemy(); } } } void attackenemy() { transform.lookat(enemy.transform); cooldown -= time.deltatime; if (cooldown <= 0) { debug.drawline(transform.position,enemy.transform.position,color.red); instantiate(bullet,vector3.forward,quaternion.identity); print("attack enemy"); cooldown = 5; } } }
i have cool down var shoot every 5 second awesome.
you close, need change line:
instantiate(bullet, vector3.forward, quaternion.identity);
to this:
private const int spawn_distance = 5; instantiate(bullet, transform.position + spawn_distance * transform.forward, transform.rotation);
quaternion.identity refers to:
this quaternion corresponds "no rotation".
Comments
Post a Comment