For the first coding task, I aim to create a gun that shoots bullets. The bullets must interact and move the 3D objects in the scene. The gun will use the instantiate feature and the add-force feature to move the bullet. First, I will create the gun then the bullet. The gun will have a script for instantiating the bullet and the bullet will have a script to add force.Â
Here is the gun it is simple but it works for the task at hand.
I made sure to add a fire point onto the gun where the bullet will be
Next, I made the bullet, which is just a sphere with a rigidbody. this is so I can add force using the rigid body.
Here is the start of the gun script, I first added the fire point and bullet game object to the script. After that in the update function, I made it so that if the fire button was pressed then instantiate the bullet prefab at the fire point.
Here is the result as you can see it spawns the bullets and they are affected by gravity. The bullets are accurate and there are no issues with the objects.
Next to the shooting script, I added the bullet speed float. Then, I changed the instantiate to a game object so I could add force. I add the force by getting the game object's rigid body and adding a force on the transform forward vector.
For the bullet script, I added a simple function in the on-collision enter which destroys the object a few seconds after the collision.
After all of this the final task is complete. I created a tower of the blocks to shoot and below is a video of me shooting the objects.