Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ItzChris92 · May 23, 2017 at 10:40 PM · c#scripting problemscripting beginnerprojectiledamage

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!

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zooow · May 24, 2017 at 03:11 AM 0
Share

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.

avatar image ItzChris92 · May 24, 2017 at 07:10 AM 0
Share

That is what I want to do... but the reason I posted was because I don't know how to do it

avatar image ItzChris92 · May 24, 2017 at 07:14 AM 0
Share

I do not know how to tell the projectile which weapon is equipped

avatar image SpaceManDan · May 24, 2017 at 12:15 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

378 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges