Hello everyone, Today we are going to have a look at shooting in unity using raycast. In this blog, we are going to discuss about raycast shooting, adding muzzle flash, force to the object, creating the target script and shattering the target finally…
So lets get started..
What i expect you guys to know already :
- Basic skills on navigating around unity using its interface…
- very simple knowledge about C# programming language such as what are integer, float and physics….
if you guys are brand new to unity then i am afraid to say this blog may not make sense to you guys.. But you can always read it ..
lets get started..
at first go to the project panel in unity and create a script called gun script…
open it up in Mono-develop or visual studio, whatever you have set as your preferences..
Since we are going implement ray-cast shooting, we need to know what exactly the raycast shooting is, Hope this demonstration makes some kind of sense to it.
In raycast shooting, we basically shoot a invisible ray from the main camera to the direction we are facing, and if that ray hits anything(any game object with collider attached in it), we are going to fetch the data from impact point of that object and store it in RaycastHit variable, otherwise we will just assume that we shot in thin air and we will do nothing.
please have a look at the video below about how to code ray-cast shooting in unity, Though there is not any sound in the video, I have tried my best to make the process understandable by commenting what i am doing in every important lines. I am sure you can understand it.
And you can download both Gun and Target script from here:
Target Script: (click on the name to download)Attach this script to the target (The object you want to damage, in the scene, You need to have a destroyed version of the object for the shattering to work perfectly as in the video)
Gun Script : (attach this script to the gun and reference the player’s camera (Main camera in inspector))
Attach the Gun script to the gun graphics in your scene and reference the Main camera inside the inspector in Gun script. You also need to make a muzzle flash for muzzle flash to take place.
For this first create a empty game object and name it fire-point and set it as child of your gun graphics and place it right in front of the bullet hole opening of the gun. Now import the default particle system package of unity and inside particle systems prefab folder, you will find a flare prefab. Drag it to the scene and make it the child of Fire point that we previously created. Now delete the sparks which is child of flare game object and name flare to muzzle flash for convenience. Now decrease the lifetime of smoke to as low as 0.1 and max particles to about 500-600. Now for emission set it to be burst and keep that between 1-5 . You will have a simple muzzle flash ready for your gun.
For impact effect, pick up the same flare particle system and reduce the lifetime of both spars and smoke to as low as 0.5 and disable play on awake, You will have a hit effect prefab.
Please watch the video for further details, Though it doesn’t has any sound, i have commented on almost every line of code to make it as understandable as possible. if you have the basic knowledge of using C# in unity, i bet you will easily understand it.
Thank you, and i will soon be back with prefab shooting. And yes if you have any queries or need any help on scripting inside of unity please leave me a message at email@example.com or click on the facebook tab in menu option, it will take you to my facebook page, Feel free to leave me a message and i will respond as soon as possible.