- Home /
shooting multiple projectiles at enemies
hi again..
i need some help on this topic here...
i'll post a youtube link and describe it as well...
http://www.youtube.com/watch?v=C4iO1Gl7wiA
its from Tom Calancy's HAWX..
i want to create multiple bullets/ missiles/ etc.. to my space ship..
similar to the youtube clip..
does anyone have any ideas on doing this??
thanks for reading.. and your patience is highly appreciated..
Answer by hijinxbassist · Jun 20, 2012 at 08:48 AM
All shots are 'instances' meaning a "shallow copy" of the original. They reference the same /mesh,position,rotation,etc/ of the original but will not be as costly as a "New" version of that prefab. In this case you will have one MAIN version of the shot (var myRocket) which can now be used in (var rocket=Instantiate(myRocket,'somePosition',Quaternion.identity) which makes the shallow copy, not quite as processor intensive as creating that same object from scratch with the same mesh,position,rotation as the original. In doing so you create a new way for unity to look at this "new" object(reference to the original), instead of looking at it as a completely new object entirely. This save speed and lines of code, and will be able to create 10 instances faster than 1 new org object of the same type.
If you are shooting on one script...say something like the previous idea
var myRocket:Transform;
function Shoot()
{
var rocket=Instantiate(myRocket,transform.position,Quaternion.identity);
//rocket.doSomethingElse;
//$$anonymous$$aybe make a var on rocket true, or speed faster, or any number of things
//be creative but be logical in doing so(fill Shoot overload with speed etc..
}
thanks... i'll try it.. but i need some more reference and some snippet codes or something..
appreciate it @hijinxbassist..
Since you dont have any code in your questions its tough to guess the proper coding technique. This is a basic structure which i provide, here is a little more on top of that..again tough to give specifics w.o actually code present
//On Player
//cur shot is deter$$anonymous$$ed from a series of actions
//the cur shot may be 1,2,3,etc if the weapons are
//set as so(maybe) add(if input getButton(1) curShot="SomeShot")
//i cant be more specifice w.o specific code
//canFire is a boolean to dealy shot time(if true plr can shoot)
function Update()
{
if(Input.GetButtonDown("Fire")
{
if(canFire)Shoot(curShot,2);
}
}
function Shoot(shotType:Transform,shotLife:float)
{
var shot=Instantiate(shotType,transform.forward,transform.rotation);
shot.GetComponent(Shot).shotLife==shotLife;//$$anonymous$$akes the shotLife 2
//granted shotType has a Comonent(Shot) and a variable called shotLife
//the basic idea is:(shotLife==how long shot is active in scene)
//which is define on a separate script(Shot) which is attached to
//the curShot(a variable which may change, num1=pistol, num2=shotgun)
}