Articles, Blog

Unity3D Tutorials – 2D Weapon change method

This tutorial is for 2D top-down shooters and
can be used in platform shooters as well Before we get into scripts i’ll
show how “main character” was built Arms have 2 components attached Rigidbody2D and HingeJoint2D And each arm has SpriteRenderer In Arms Rigidbody2D component i’ve checked the FixedAngle and IsKinematic With these selected my arms wont be affected by built-in physics In the Resources folder i’ve created prefabs
of 2 weapons “Onehanded” pistol and
“Twohanded” rifle Every weapon has Rigidbody2D with FixedAngle checked Now when all GameObjects are properly configured it’s time to create scripts But before that we need some kind of a plan
on how our scripts will communicate/behave My plan is to make one script for weapons
where we can set “weapon type”, fire rate etc. And another one for arms to change sprites in dependency of current weapon type I’ll use 2 “weapon types” and call them OneHanded and TwoHanded Just to explain how scripts will correspond with each other Now when I know exactly how many weapon type. I know that I need 3 script files 1. WeaponScript
2. ArmsScript
3. Enumerations Let’s start with Enumerations because it’s the “smallest” one Since this file will contain only my enumerations I can delete whole code inside Now with clear file I can create enum with my weapon types Thats all we need in here so save this and proceed to another file Next file that I want to edit is WeaponScript In this script I want to have 3 properties that can be changed in the inspector view so i can get rid of the Start method And in case i would want to update some of those properties from different script I’ll use LateUpdate insteed of Update Now I need to create variables of type WeaponType, Vector2 and Quaternion Vector2 will be used to set anchor in my HingeJoint2D component And Quaternion to adjust rotation of weapon in dependency of Arms rotation In the LateUpdate method I need to simply assign variable myRotation to objects transform.localRotation Save this script and pin it into each weapon prefab Those errors shows that there are missing MonoBehaviour in GameObject. To fix this just check for unattached script in GameObject. When scripts are attached to all weapons I can edit their properties No all I need to do is find proper value for HingeJoint2D’s anchor for each weapon To do this attach weapons Rigidbody2D into HingeJoint2D’s Connected RigidBody property
Hit play and tweak anchor values For this example values of x=0.3 and y=0.07 are good enough so I’ll just rewrite them into WeaponScript property When values are set just save the prefab Since I do not have script for changing sprites for one and two -handed weapons I’ll open up my VisualStudio and write this script First I need refference to both hands and to sprites for OneHanded and TwoHanded weapons I want to be able to set them up from inspector view so I make them public Okay so refferences to hands are done.
Now I have to set refference to Sprites. When hands are done I want to be able to know what weapon i have currently equipped In that case I’ll create 3 private variables of type GameObject, WeaponScript and HingeJoint2D CurrentWeapon will be refference to currently equipped weapon myJoint will be refference to HingeJoint2D component CurrentWeaponScript will be refference to CurrentWeapon’s WeaponScript After that I want to check if there is HingeJoint2D component attached to my Arms and if theres not then attach it. Quick explanation of how GetComponent “works” When that’s done I have to check if there is any weapon equipped and if not – equip some CurrentWeapon is private variable so when the game starts it has to be null just like any other variable without assigned value So I could write this if-statement as ‘if(true)’ When I cleared this out it’s time to Instantiate weapon prefab and get it’s values Starting of assigning Instantiated weapon object to the CurrentWeapon variable Note that I have my prefabs stored in Resources folder so I can use Resources.Load method Quick explanation of Instantiate method. If I remember well I can instantiate object as a Transform, object or GameObject by declaring it’s type like I presented here Object is the default “type” But I cant Instantiate object of other type eg. Rigidbody2D Next thing that I have to assign is CurrentWeaponScript and I’ll do this by getting component from Instantiated weapon Now I’m curious about something that I’ve read in some kind of “tutorials”. There was something about passing an argument : ‘GetComponent(arg here)’ this should get component with index number of arg like when we have 3 colliders and we pass 1 the second in hierarchy would be choosen. And looks like it does not work 😉 Now I have to change value of my HingeJoint2D’s anchor to weapon’s offset value Basic properites are set so I want to check how it works before going any further. Something is wrong so check check what could it be. To make it clearer I’ll add position and rotation of instantiated weapon and insert it to HingeJoint2D connected rigidbody property When that’s done I want to check what WeaponType is this weapon and change to proper sprites I’ll use if-statement but this can be done easily with switch Inside this if-statement I have to check if each of my arms have proper sprite and if it does not change it Then do the same with other types of weapons After all of that copy & paste I have to set up this script in the Inspector view and see if it’s working Now I know that it’s working with attaching weapon to my hand I need to be able to rotate weapon as well Let’s add that to the script. I’ve already create this in WeaponScript so I just need to make it inside of ArmsScript Quick explanation on how to prevent weird errors 😉 Rotation applied, Let’s check if it works as planned Seems that it’s working so now I want to create method that will switch weapons when I press key 1 or 2 Now I need to cut code from Start method and paste it into new method to be able to call it after I press switch weapon button In this method I need something to check if choosen weapon exists in Resources folder When that’s done I need to call this method inside my Start method, Add key to switch to my rifle and tweak WeaponScript values in my Rifle While adjusting Rifle’s WeaponScript I’ve noticed that previous weapon was not destroyed so I will add that now. All that’s left to do is adjust some values and run some more tests

2 thoughts on “Unity3D Tutorials – 2D Weapon change method

Leave a Reply

Your email address will not be published. Required fields are marked *