Damage calculation with various weapons
I've recently implemented a weapon swap system and it has raised an issue with the damage calculation (only on a prototype and this was expected but I'm a unity noob so just went with it until now). I'm not getting any errors at the moment but I know the code will not work as I add more projectile guns.
My player now has a 'Weapon Holder' empty game object, that holds a maximum of 3 guns, and a minimum of 1. The weapon swap button simply calls SetActive on the various guns in sequence to make it appear that the player is changing weapons. I don't really know how to adpat my code to work for this.
Here is the code I am using for calculating damage to the enemy at the moment (these methods are on an EnemyHP script) I have 2 seperate functions for this which I know is terrible, but this was my work around for using 2 different weapons:
Projectile Guns:
public void UpdateHealthPR () //Method for updating health if gun used is a projectile gun
{
var enemyDef = Random.Range (gameObject.GetComponent<Armor> ().minArmor, gameObject.GetComponent<Armor> ().maxArmor);
var playerAtk = Random.Range (GameObject.FindGameObjectWithTag("Player").GetComponentInChildren<ProjectileGun>().minGunDamage, GameObject.FindGameObjectWithTag("Player").GetComponentInChildren<ProjectileGun>().maxGunDamage);
curHP -= (playerAtk - enemyDef);
}
Raycast Guns:
public void UpdateHealthRC() //Method for updating health if gun used is a raycast
{
var enemyDef = Random.Range (gameObject.GetComponent<Armor> ().minArmor, gameObject.GetComponent<Armor> ().maxArmor);
var playerAtk = Random.Range (GameObject.FindGameObjectWithTag("Player").GetComponentInChildren<RaycastGun>().minGunDamage, GameObject.FindGameObjectWithTag("Player").GetComponentInChildren<RaycastGun>().maxGunDamage);
curHP -= (playerAtk - enemyDef);
}
How can I change this to allow it to be dynamic for any weapons that may be equipped? The player will be able to change his loadout before he starts a game, so it cannot reference by name... If someone can help me with this then I imagine it will be very easy to combine the above 2 functions into one. Any help would be very much appreciated and thanks in advance!
What i would do is to have damage type (enum) and damage amount (float) told to the instantiated projectile (bullet or laser beam). So when the projectile collide with the enemy, the enemy could know the damage type and amount and deal with it.
So basically when player press "fire", the player class instantiate a projectile. As the player knows which weapons is currently equipped, you can pass this damage type and damage amount to the projectile class.
That is what I want to do... but the reason I posted was because I don't know how to do it
I do not know how to tell the projectile which weapon is equipped
Oh boy, you need to step back a bit to simplify. $$anonymous$$y suggestion is to create a Player script and make it a singleton. Here is an example of what I mean and how to use it.
public class Player : $$anonymous$$onoBehavior
{
public static Player Instance;
private void Start()
{
Instance = this;
}
public CalculateAttack()
{
//Do Stuff;
}
}
Now you can reference Player from other scripts by using Player.Instance, like so:
Player.Instance.CalculateAttack();
This isnt all the things you need to get what you are asking to work, but it will get you a few steps forward I think. Hope this helps.
Answer by Zooow · May 24, 2017 at 12:18 PM
Make a "bullet" prefab with a "Bullet.cs" class object on it. When you instantiate your bullet prefab, get its Bullet component and pass it some informations. For instance the script below "shoot" a bullet when player press left mouse button. Once the bullet is instantiated, the function Shoot() is used to store spawn position, direction and damage informations in the Bullet itself.
if(Input.GetMouseButtonDown(0))
{
if(prefabBullet)
{
GameObject go = Instantiate(prefabBullet);
Bullet bu = go.GetComponent<Bullet>();
if(bu)
{
Vector2 target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
bu.Shoot(rb2D.position, target, DamageType, DamageAmount);
}
}
}
then when your bullet collide with an enemy, get the bullet component and get the damageType and damageAmount information.
For a more complete tutorial about shooting projectiles, there's the unity tutorial here maybe : https://unity3d.com/fr/learn/tutorials/projects/space-shooter-tutorial
Your answer
Follow this Question
Related Questions
Help with Script 0 Answers
How to make a thrown object land on a certain point e.g a thrown spear landing on its tip 0 Answers
Click on Animation script throughout the game 0 Answers
Having trouble deleting objects from a list after they reach a certain scale. 0 Answers
Accessing string from other script 1 Answer