public class Weapon : MonoBehaviour
{
public float normalDamage;
public float fireRate;
public float damage = 5;
public float range;
public float radius;
public float reloadTime;
public float fireTimer;
public float delay;
public float basedamage;
}
public class Projectile : MonoBehaviour
{
void OnCollisionEnter(Collider other)
{
if(other.GetComponent<EnemyHealth>() != null)
{
other.GetComponent<EnemyHealth>().TakeDamage(damage);
}
}
}
How do I get the damage from my weapon scripts to work in my projectile script. I’ve tried so many
other methods and it just not working.
Here needs be considered the architecture and logic. Most likely a Weapon should include a projectile as it is the logical way of things (You use a certain type of bullet with a given rifle in real life, not the other way around).
So your weapon class would include the Projectile it is using. This way you can pass how much damage a projectile should do based on what weapon is using it, just like in real.
Your weapon class should handle the input so when user presses a button, a projectile is created (or used from a pool) and the damage value is given to it.
public class Weapon : MonoBehaviour
{
[SerializedField] protected GameObject projectile;
[SerializedField]protected float damage;
protected virtual void Update(){
if(Input.GetKeyDown(KeyCode.Space)){ CreateProjectile(); }
}
protected virtual void CreateProjectile(){
GameObject obj = (GameObject) Instantiate(this.projectile);
obj.GetComponent<Projectile>().Init(this.damage);
}
}
pubic class Projectile : MonoBehaviour
{
[SerializedField] protected float initialDamage;
protected float actualDamage;
public virtual void Init(float weaponDamage){
// Here we could say that projectile has initial damage that is multiplied by weapon damage
this.actualDamage = this.initialDamage * weaponDamage;
}
protected virtual void OnCollisionEnter(Collider other)
{
EnemyHealth ea = other.GetComponent<EnemyHealth>();
if(ea == null){ return; }
ea.TakeDamage(this.actualDamage);
}
}
Notice that most methods and references are protected and/or virtual. This is because you should make your Weapon and Projectile classes as abstract. You don’t find a weapon in real, you find a AK-47 that is a weapon. So you could consider a subclass Ak that inherits from Weapon and takes care of its own data.
As for the code, when the space bar is pressed down, it will create a new projectile and on creation, it will pass down some data (there could be more than just a float). This is simple but could get you going in a good direction.
I am assuming your Weapon class can make things like Axes, Swords, and projectile throwing weapons like Arrow/Bow and Throwing Knives. If this is a correct assumption then you can do something like this:
public class Projectile : MonoBehaviour
{
// Get a reference to the Weapon that created this Projectile. You would set this in the inspector.
public Weapon weapon;
// Since the variables in your Weapon class are public you can call on them like this:
Debug.Log(weapon.damage);
void OnCollisionEnter(Collider other)
{
if(other.GetComponent<EnemyHealth>() != null)
{
other.GetComponent<EnemyHealth>().TakeDamage(weapon.damage);
}
}
}