Sunday 24 January 2016

Asteroids in Unity pt 2

Now we have the ship and scene all finished, we can get to work on some real coding and getting a working bullet script in place, as well as adjusting the PlayerController script we made beforehand.

To begin I imported a laser png and gave it both a rigidbody and a box collider, these ensure that the bullet reacts when it comes in contact with another object such as an asteroid. I then saved the work on the bullet and put it in a prefab folder for future use, this means that I wont have to put the rigidbody or box collider on again.


Then I created a script for the bullet which basically consisted of just the speed controls. In the playercontroller script I coded in the basics for the bullet, this began with simply pressing a key, (space in my case), which made a bullet appear in a static location and fly horizontally until it left the playable area. 

I then coded it so the bullet would spawn on the spaceship and would fly in the direction the ship was facing using the code, "Instantiate (bulletPrefab, transform.position, transform.rotation);".


At this point the bullets were firing out of the ship, but were colliding with everything and fired too many bullets at once. I fixed this by changing the box collider settings to 'is trigger', this stopped any collision between the bullet and the ship.

The last thing I needed to do was change the layer order so the bullet looked like it fired out the front of the ship instead of just appearing from on top of the ship.




 

No comments:

Post a Comment