Forum begins after the advertisement:
[Part 8] Damage multiplier for might is working weird.
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 8] Damage multiplier for might is working weird.
- This topic has 4 replies, 2 voices, and was last updated 9 months, 3 weeks ago by Renaud St-Hilaire.
-
AuthorPosts
-
February 27, 2024 at 1:01 am #13415::
To have the weapons do 10 damage instead of five I need to set the multiplier of the passive item scriptable object to 900. Might is set to 1 and damage is set to 5. When I start the game the might goes up to 10 and I do 10 damages. Its not multiplying the damage value of the weapons somehow but taking the might value and applying that for damages. Your help is greatly appreciated.
February 27, 2024 at 6:24 pm #13417::Renaud, can you share a screenshot of the components set to the values you mentioned? As well as your
PlayerStats
and all affectedWeaponBehaviour
scripts.February 29, 2024 at 10:30 pm #13428::Here it is
<code>using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerStats : MonoBehaviour { CharacterScriptableObject characterData; //current data public float currentHealth; [HideInInspector] public float currentRecovery; [HideInInspector] public float currentMovementSpeed; public float currentMight; [HideInInspector] public float currentProjectileSpeed; [HideInInspector] public float currentMagnet; //Spawned Weapon public List<GameObject> spawnedWeapons; //Experience and level of the player [Header("Experience/Level")] public int experience = 0; public int level = 1; public int experienceCap; //Class for defining a level range and the corresponding experience cap increase for that range [System.Serializable] public class LevelRange { public int startLevel; public int endLevel; public int experienceCapIncrease; } //I-Frames [Header("I-Frames")] public float invincibilityDuration; float invincibilityTimer; bool isInvincible; public List<LevelRange> levelRanges; void Awake() { characterData = CharacterSelector.GetData(); CharacterSelector.instance.DestroySingleton(); //assign the variables currentHealth = characterData.MaxHealth; currentRecovery = characterData.Recovery; currentMovementSpeed = characterData.MovementBaseSpeed; currentMight = characterData.Might; currentProjectileSpeed = characterData.ProjectileSpeed; currentMagnet = characterData.Magnet; //Spawn the starting weapon SpawnedWeapon(characterData.StartingWeapon); } private void Start() { //initializes the experience cap as the first experience cap increase experienceCap = levelRanges[0].experienceCapIncrease; } private void Update() { if(invincibilityTimer > 0) { invincibilityTimer -= Time.deltaTime; } // if the invincibilty timer has reached 0, set the invincibilty flag to false else if (isInvincible) { isInvincible = false; } Recover(); } public void IncreaseExperience(int amount) { experience += amount; LevelUpChecker(); } void LevelUpChecker() { if(experience >= experienceCap) { level++; experience -= experienceCap; int experienceCapIncrease = 0; foreach (LevelRange range in levelRanges) { if(level >= range.startLevel && level <= range.endLevel) { experienceCapIncrease = range.experienceCapIncrease; break; } } experienceCap += experienceCapIncrease; } } public void TakeDamage(float dmg) { //If the player is not currently invincible, reduce health and start invincibility if (!isInvincible) { currentHealth -= dmg; invincibilityTimer = invincibilityDuration; isInvincible = true; if (currentHealth <= 0) { Kill(); } } } public void Kill() { Debug.Log("Player is Dead"); } public void RestoreHealth(float amount) { //Only heal the player if their current health is less than their maximum health if(currentHealth < characterData.MaxHealth) { currentHealth += amount; //make sure the player's health doesn't exceed their max health if(currentHealth > characterData.MaxHealth) { currentHealth = characterData.MaxHealth; } } } void Recover() { if (currentHealth < characterData.MaxHealth) { currentHealth += currentRecovery * Time.deltaTime; //Make sure the player's health doesnt exceed their maximum health if(currentHealth > characterData.MaxHealth) { currentHealth = characterData.MaxHealth; } } } public void SpawnedWeapon(GameObject weapon) { //Spawn the starting weapon GameObject spawnedWeapon = Instantiate(weapon, transform.position, Quaternion.identity); spawnedWeapon.transform.SetParent(transform); //set the weapon to be a child of the player spawnedWeapons.Add(spawnedWeapon); //add it to the list of spawned weapons } }</code>
<code>using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// Base script of all projectile behaviors [to be placed on a prefab of a weapon that is a projectile /// </summary> public class ProjectileWeaponBehaviour : MonoBehaviour { public WeaponScriptableObject weaponData; protected Vector3 direction; public float destroyAfterSeconds; //Current stats protected float currentDamage; protected float currentSpeed; protected float currentCooldownDuration; protected int currentPierce; void Awake() { currentDamage = weaponData.Damage; currentSpeed = weaponData.Base_speed; currentCooldownDuration = weaponData.CooldownDuration; currentPierce = weaponData.Pierce; } public float GetCurrentDamage() { return currentDamage = FindObjectOfType<PlayerStats>().currentMight; } // Start is called before the first frame update protected virtual void Start() { Destroy(gameObject, destroyAfterSeconds); } public void DirectionChecker(Vector3 dir) { direction = dir; float dirx = direction.x; float diry = direction.y; Vector3 scale = transform.localScale; Vector3 rotation = transform.rotation.eulerAngles; if (dirx < 0 && diry == 0) //left { scale.x = scale.x * -1; scale.y = scale.y * -1; } else if (dirx == 0 && diry < 0) //down { scale.y = scale.y * -1; } else if (dirx == 0 && diry > 0) //up { scale.x = scale.x * -1; } else if (dirx > 0 && diry > 0) //right up { rotation.z = 0f; } else if (dirx > 0 && diry < 0) //right down { rotation.z = -90f; } else if (dirx < 0 && diry > 0) //left up { scale.x = scale.x * -1; scale.y = scale.y * -1; rotation.z = -90f; } else if (dirx < 0 && diry < 0) //left down { scale.x = scale.x * -1; scale.y = scale.y * -1; rotation.z = 0f; } transform.localScale = scale; transform.rotation = Quaternion.Euler(rotation); //Can't simply set the vector because cannot convert } protected virtual void OnTriggerEnter2D(Collider2D col) { //Reference the script from the collided collider and deal damage using TakeDamage() if (col.CompareTag("Enemy")) { EnemyStats enemy = col.GetComponent<EnemyStats>(); enemy.TakeDamage(GetCurrentDamage()); //Make sure to use currentDamage instead of weaponData.Damage in case any damage multipliers in the future ReducePierce(); } else if (col.CompareTag("Prop")) { if (col.gameObject.TryGetComponent(out BreakableProps breakable)) { breakable.TakeDamage(GetCurrentDamage()); ReducePierce(); } if (col.gameObject.TryGetComponent(out BreakableProps2 breakable2)) { breakable2.TakeDamage(GetCurrentDamage()); ReducePierce(); } } } void ReducePierce() //Destroy once the pierce reaches 0 { currentPierce--; if (currentPierce <= 0) { Destroy(gameObject); } } }</code>
<code>using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// Base script of all melee behaviours [To be placed on a prefab of a weapon that is melee] /// </summary> public class MeleeWeaponBehaviour : MonoBehaviour { public WeaponScriptableObject weaponData; public float destroyAfterSeconds; //Current stats protected float currentDamage; protected float currentSpeed; protected float currentCooldownDuration; protected int currentPierce; void Awake() { currentDamage = weaponData.Damage; currentSpeed = weaponData.Base_speed; currentCooldownDuration = weaponData.CooldownDuration; currentPierce = weaponData.Pierce; } public float GetCurrentDamage() { return currentDamage = FindObjectOfType<PlayerStats>().currentMight; } protected virtual void Start() { Destroy(gameObject, destroyAfterSeconds); } protected virtual void OnTriggerEnter2D(Collider2D col) { if (col.CompareTag("Enemy")) { EnemyStats enemy = col.GetComponent<EnemyStats>(); enemy.TakeDamage(GetCurrentDamage()); } else if (col.CompareTag("Prop")) { if (col.gameObject.TryGetComponent(out BreakableProps breakable)) { breakable.TakeDamage(GetCurrentDamage()); } if (col.gameObject.TryGetComponent(out BreakableProps2 breakable2)) { breakable2.TakeDamage(GetCurrentDamage()); } } } }</code>
March 1, 2024 at 1:39 am #13429::You are missing the * in your code for both your GetCurrentDamage() functions in both weapons:
public float GetCurrentDamage() { return currentDamage *= FindObjectOfType<PlayerStats>().currentMight; }
Without the
*
you are setting thecurrentDamage
variable directly.March 1, 2024 at 2:10 am #13430 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: