I come from an action script background, and am trying to make a simple spaceship shooter game to learn some of the essentials of JavaScript.
So far I have the movement of the character down, but I am struggling to find the efficient way to create bullets and then remove them when either they hit an opponent, object, or fly off the screen.
So if someone could either explain or point me in the right direction I would appreciate it.
So I need the following:
1)How to create instances of objects.
2)How to do a hit test with that object.
3)How to keep track of the created objects(Thinking arrays.)
I would really really suggest u read the FPS tutorial of unity. www.unity3d.com
there are scripts which make the bullets. how to create them, make them appear, on collision destroy and so one…
So dont be lazy and go read true that…
Have fun id say
You can do this with Object.Instantiate. You basically start with a “template” object and then make copies of it with this function.
You can implement a function in your script called OnCollisionEnter that gets called whenever an object with a collider comes into contact with another collider. You could add this function to the bullet script and destroy the bullet as it hits something.
Bullets are generally destroyed in the order they are created (ie, a bullet created later doesn’t normally hit something before one created earlier) so the Array class’s Push, Pop, Shift, Unshift are a straightforward way to keep track of bullet objects.
Create an empty gameobject, parent it to the player model and position it a couple units in front of him. Then make the bullets spawn from that, instead of him.