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 15, 2017 at 12:22 PM · c#scripting problemdamagerangeprojectiles

Weapon range/damage with a projectile

Currently I have a GunBaseClass that my projectile weapons inherit from. However, I am wondering how I can set the range of a gun on the gun itself instead of the projectile. Currently the projectile has a rigidbody with a velocity, and will destroy itself after x amount of time, so to set the range I have to fiddle with these numbers. Also, the same goes for weapon damage... this again has to be set on the projectile (and so the projectile has to inherit from the GunBaseClass).

I want it so that my projectile is simply an object that hits the enemy, but has no stats of its own, as all of these should be on the weapon, and so that the projectile doesn't need to inherit from the GunBaseClass. How could I do this? Thanks in advance for the help! I'll update on here if I fix the issue

Gun Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ProjectileGun : GunBaseClass {
 
     public Transform muzzleTip;
     public Light muzzleFlashLight;
 
     private float lastShot = 0.0f;
 
     void Update()
     {
         if (JoystickFire.instance.Fire && Time.time > fireRate + lastShot) {
             Shoot ();
             muzzleFlashLight.enabled = true;
         } 
         else 
         {
             muzzleFlashLight.enabled = false;
         }
     }
 
     void Shoot()
     {
         GameObject bullet = ObjectPooler.SharedInstance.GetPooledObject ("Player Bullet"); 
         if (bullet != null) 
         {
             bullet.transform.position = muzzleTip.position;
             bullet.transform.rotation = transform.rotation;
             bullet.SetActive (true);
             Debug.Log("Shot Fired");
         }
         lastShot = Time.time;
     }
 }

Projectile Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Projectile : ProjectileGun { //gunDamage needs fixing as it currently has to be set on the projectile, not the gun
 
     public float timer = 0.5f; //range = speed * timer (**Set in inspector**)
     public float speed = 60f; //range = speed * timer (**Set in inspector**)
 
     void OnEnable()
     {
         Invoke ("Die", timer);
     }
 
     void Update()
     {
         transform.position += transform.forward * speed * Time.deltaTime;
     }
 
     void OnBecameInvisible ()
     {
         gameObject.SetActive (false);
     }
 
     void OnCollisionEnter(Collision enemy)
     {
         
         if (enemy.gameObject.tag == "Enemy") 
         {
             EnemyBaseClass sn = enemy.gameObject.GetComponent<EnemyBaseClass> ();
             sn.enemyCurrentHealth -= Random.Range (minGunDamage, maxGunDamage) /*- sn.enemyArmorRating*/; 
             Debug.Log ("Enemy Current Health: " + sn.enemyCurrentHealth);
             gameObject.SetActive (false);
         }
     }
         
     void Die()
     {
         gameObject.SetActive(false);
     }
 }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by alankemp · May 15, 2017 at 03:46 PM

You could move the timer and speed public variables from the projectile to the gun class so you can set them there in Unity.

Add an initialization function in your projectile class that takes the time to live and speed as parameters and saves them into private variables in the projectile class.

When you spawn the bullet, call the initialization function passing the timer and speed values.

As an aside,

 class Projectile : ProjectileGun

This is a bit strange, why are your projectiles derived from your guns? You should inherit from a class when the new class is a more specialized type of the base class.

Comment
Add comment · Show 3 · 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
avatar image ItzChris92 · May 15, 2017 at 04:40 PM 0
Share

Thanks for the reply, but I'm not too sure on how to initialize variable in another script using a method. I don't expect the code written for me, but what is this called so I can give it a search?

Also, the whole reason I asked the question is because I didn't like the fact that my projectile class was having to inherit from the Projectile Gun just so I could apply the same variables :)

avatar image alankemp ItzChris92 · May 15, 2017 at 06:39 PM 0
Share

You could change your projectile class to derive from $$anonymous$$onoBehaviour like this:

 class Projectile : $$anonymous$$onoBehaviour

$$anonymous$$ove the public speed and timer variables from the projectile class to the gun class.

Then add two private variables to the projectile class and write a new function in the projectile class to initialise it:

 private float $$anonymous$$yTimer;
 private float $$anonymous$$ySpeed;
 
 void Initialize(float timer, float speed)
 {
     $$anonymous$$yTimer = timer;
     $$anonymous$$ySpeed = speed;
 }

In your gun class shoot function, inside the if (bullet != null), you can call this new function:

          if (bullet != null) 
          {
              bullet.Initialize(timer, speed);
              bullet.transform.position = muzzleTip.position;
              bullet.transform.rotation = transform.rotation;
              bullet.SetActive (true);
              Debug.Log("Shot Fired");
          }

You will need to change the places in the projectile script that use the old variable names to use these new ones ($$anonymous$$yTimer and $$anonymous$$ySpeed), or rename them to something else that makes sense to you.

Now you have the speed and the timer in the gun class where it belong, and each new projectile you create will get told those values.

avatar image ItzChris92 · May 16, 2017 at 03:07 PM 0
Share

Thank you!

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

366 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

Related Questions

Auto Set vs Manual Decrease (How to do both?) 1 Answer

Damage calculation with various weapons 1 Answer

Why are my instantiated projectiles not firing? 0 Answers

I need help with my Damage + GameOver Script 0 Answers

why StartCoroutine(SpawnBigTree()); don't work and the float has a error 0 Answers


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