Forum begins after the advertisement:
[part 5] player not taking damage
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [part 5] player not taking damage
- This topic has 12 replies, 2 voices, and was last updated 5 months, 3 weeks ago by Terence.
-
AuthorPosts
-
April 11, 2024 at 5:37 pm #13832C VagdaltParticipant::
i am almost done with part 5 of the series and my player is supposed to take damage whne getting hit by the enemy but nothing happens.
this is my playerstats script:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerStats : MonoBehaviour { public CharacterScriptableObject characterData; //current stats float currentHealth; float currentRecovery; float currentMoveSpeed; float currentMight; float currentProjectileSpeed; //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 ca 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; private float currentDamage; void Start() { // initialize the experience cap as the first experience cap increase experienceCap = levelRanges[0].experienceCapIncrease; } void Update() { if(invincibilityTimer > 0) { invincibilityTimer -= Time.deltaTime; } else if(isInvincible) { isInvincible = false; } } 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 (!isInvincible) { currentHealth -= dmg; invincibilityTimer = invincibilityDuration; isInvincible = true; if (currentHealth <= 0) { Kill(); } } } public void Kill() { Debug.Log("Player is dead"); } private void OnCollisionStay2D(Collision2D col) { if (col.gameObject.CompareTag("Player")) { PlayerStats player = col.gameObject.GetComponent<PlayerStats>(); player.TakeDamage(currentDamage); } } }
April 11, 2024 at 7:55 pm #13833TerenceKeymaster::Vagdalt, this function shouldn’t be in
PlayerStats
. It should be inEnemyStats
.private void OnCollisionStay2D(Collision2D col) { if (col.gameObject.CompareTag("Player")) { PlayerStats player = col.gameObject.GetComponent
(); player.TakeDamage(currentDamage); } } April 12, 2024 at 3:51 pm #13841C VagdaltParticipant::i put it in enemystats but it still doesnt work.
ill also post enemystats here s you can see if everything is alright there, the scripts doesnt show any errors
ENEMYSTATS:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyStats : MonoBehaviour { public EnemyScriptableObject enemyData; //Current stats float currentMoveSpeed; float currentHealth; float currentDamage; public void Awake() { //Assign the vaiables currentMoveSpeed = enemyData.MoveSpeed; currentHealth = enemyData.MaxHealth; currentDamage = enemyData.Damage; } public void TakeDamage(float dmg) { currentHealth -= dmg; if (currentHealth <= 0) { Kill(); } } public void Kill() { Destroy(gameObject); } private void OnCollisionStay2D(Collision2D col) { if (col.gameObject.CompareTag("Player")) { PlayerStats player = col.gameObject.GetComponent<PlayerStats>(); player.TakeDamage(currentDamage); } } }
PLAYERSTATS:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerStats : MonoBehaviour { public CharacterScriptableObject characterData; //current stats float currentHealth; float currentRecovery; float currentMoveSpeed; float currentMight; float currentProjectileSpeed; //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 ca 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() { currentHealth = characterData.Maxhealth; currentRecovery = characterData.Recovery; currentMoveSpeed = characterData.MoveSpeed; currentMight = characterData.Might; currentProjectileSpeed = characterData.ProjectileSpeed; } void Start() { // initialize the experience cap as the first experience cap increase experienceCap = levelRanges[0].experienceCapIncrease; } void Update() { if(invincibilityTimer > 0) { invincibilityTimer -= Time.deltaTime; } else if(isInvincible) { isInvincible = false; } } 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 (!isInvincible) { currentHealth -= dmg; invincibilityTimer = invincibilityDuration; isInvincible = true; if (currentHealth <= 0) { Kill(); } } } public void Kill() { Debug.Log("Player is dead"); } }
April 12, 2024 at 5:21 pm #13842April 12, 2024 at 8:40 pm #13843April 13, 2024 at 12:32 pm #13845TerenceKeymaster::Can you add the following lines to your code and show me the Console when the player is touched by the enemy?
private void OnCollisionStay2D(Collision2D col) { print("Collision with enemy detected"); if (col.gameObject.CompareTag("Player")) { print("Collision is with player"); PlayerStats player = col.gameObject.GetComponent(); player.TakeDamage(currentDamage); } }
April 15, 2024 at 2:54 pm #13860C VagdaltParticipant::looks like it doesnt detect any collision
code looks liket his right now:
private void OnCollisionStay2D(Collision2D col) { print("Collision with enemy detected"); if (col.gameObject.CompareTag("Player")) { print("Collision is with player"); PlayerStats player = col.gameObject.GetComponent<PlayerStats>(); player.TakeDamage(currentDamage); } }
April 15, 2024 at 10:11 pm #13863TerenceKeymaster::Can you click on the error to expand it? Show me the line that the error is flagging as well (bold it).
April 16, 2024 at 3:40 pm #13880C VagdaltParticipant::it isnt showing the error anymore now that i started it up again, the problem with the player not taking damage is stil there
April 16, 2024 at 4:04 pm #13881TerenceKeymaster::Do you have a Rigidbody2D on the player or enemy? You need at least one Rigidbody2D between them for
OnCollisionStay2D()
to register.April 16, 2024 at 5:19 pm #13882April 16, 2024 at 6:56 pm #13883C VagdaltParticipant::the error is back
also saw some new ones. I made a screenie of the code in question too
April 17, 2024 at 1:55 pm #13903 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: