Part 4 of 5: Creating smart enemy characters using Unity3D - Attack player with fun weapons
Welcome to Part 4 in our series of developing a smart enemy character using Unity3D.
Here are links to the full series.
1) Patrol their region and have some fun
2) Be self-aware and determine proximity to main player
3) Run to main player
4) Attack main player
5) Manage health & die beautifully
In this blog post we will cover how to create some fun weapons and attack our player. An enemy usually has one or more of these weapons: laser, bomb, sniper gun, arrow, sword, punch, etc.
For the purpose of this tutorial we will create few such weapons:
> A simple laser gun, and
> A bomb
> A rocket gun
Laser Guns are cool
Laser guns are created using a simple line renderer.
All we need to do is create a straight line from the tip of the gun (aka muzzle) to the player position and if you draw it fast enough it will look like a laser. Here's roughly how we will draw that laser line.
// Reference to the gun muzzle tip. private var muzzleTip : GameObject; ...... // Set the initial position of the line renderer to the tip of the gun. laserShotLine.SetPosition(0, muzzleTip.transform.position); // Set the end position of the player's position. laserShotLine.SetPosition(1, player.position); ......
Let's start by creating an image that will eventually be used to create the laser effect.
1) Create a simple laser like gradient image as shown below using Photoshop (or similar tool), so that it looks similar to a star wars type laser as shown below
Dimensions: 32x32
Type: png
All you need to do is add a gradient with white in the middle
Here's a star wars robot shooting a laser, see the resemblance :)
Once the png file is ready simply drag it into your Assets folder.
2) Next create a new material to hold the image we just created.
This material will be used to shoot the laser. Let's start by applying the previously created png file to this material by clicking on "Select" and then picking the png texture that we created in Photoshop.
Once the texture is selected go ahead and save the project.
3) Create an empty gameobject and call it "lasereffect".
Then drag the material you just created and drop it on top of the "lasereffect" gameobject that we created in earlier step.
4) Assign a "Light Renderer" component to the "LaserPreFab" game object
5) Assign a "Line Renderer" component to the "LaserPreFab" game object
4) Next let's import a 3D laser gun (.fbx) file and drop it on your scene. For the purpose of this project I found one online and used it for the demo. Let's call this object, "lasergun".
5) Create an empty gameobject, let's call it "MuzzleTip".
Now position this muzzletip empty gameobject at the tip of your laser gun. You might need to use [Ctrl +v] to move the empty gameobject so that you can easily snap the empty gameobject to the exact tip.
Now since we want the laser to shoot away from the gun and towards our player we probably need to adjust the MuzzleTip z-axis (shown in blue) pointing away from the gun in positive direction.
Let's rotate the gamebject along it's y-axis by 270 deg. so that the z-axis are pointing outwards as shown below.
Then we can use the Transform.forward method to push our laser object and it will translate it along the z-axis as shown above.
Transform.forward is short-form for Vector3(0, 0, 1).
6) Let's put the lasergun in our enemy's hand.
Locate the enemy's hand and make the "lasergun" gameobject a child of the enemy's right hand.
7) Next add a script to the "LaserPreFab" -- this is where we will detect mouse press and create an instance of the Let's put the lasergun in our enemy's hand.
Locate the enemy's hand and make the "lasergun" gameobject a child of the enemy's right hand.
#pragma strict public var muzzleTipTransform : Transform; public var laserPrefab: GameObject; function Update () { // Mouse was pressed, create laser if (Input.GetButton("Fire1")) { // create the laser instance var laserInstance:GameObject = Instantiate(laserPrefab); // position the laser at the muzzletip of the gun laserInstance.transform.position = muzzleTipTransform.position; } }
Let's summarize what we've completed so far:
a) We've got an enemy in our scene (called "enemy")
b) This enemy has a laser gun (called "lasergun")
c) We've marked the tip of the laser gun by placing an empty game object (called "muzzletip")
d) We will create the laser effect by using the gameobject (called "laserguneffect" which has a line renderer and light component attached to it
7) Finally attach an audio clip so that when the laser fires it plays an interesting audio clip.
Start by importing the audio file into your Assets folder.
Next click the LaserPreFab object and add a new component "Audio Source" to it.
Now drag the audio clip you just imported onto this component.
8) Next create a new script and attach it to your "LaserPrefab" object.
#pragma strict public var speed = 5f; function Start () { } function Update () { // this will move the laser in the forward direction // as soon as it is instantiated transform.position += transform.forward * speed * Time.deltaTime; // this will destroy the gameobject in 2 seconds Destroy(gameObject, 2f); }
Rocket Guns are also fun
The mechanics of rocket gun are exactly the same as the laser gun.
You would instantiate a rocket prefab at the muzzletip of the gun.
Now because it is a rocket gun you could benefit by applying a rigidbody component to the prefab and let gravity act on it.
public var rocketPrefab : Rigidbody; public var muzzleTipTransform : Transform; function Update () { if(Input.GetButtonDown("Fire1")) { var rocketInstance : Rigidbody; rocketInstance = Instantiate(rocketPrefab, muzzleTipTransform.position, muzzleTipTransform.rotation); rocketInstance.AddForce(muzzleTipTransform.forward * 5000); } }
This completes Part 4 of our tutorial series on creating an interesting game.
We will wrap things up in our next blog - Part 5 of the tutorial series - where we will focus our attention on making our enemy die with a fun animation and create particles effects for the blood splatter.












