2d collision with damage not working.
Can somebody help me with this script as it does not inflict damage to the player as programmed, the two objects of player and enemy both collide and push against one another but the health remains the same. Both objects have rigidbody 2d and 2d colliders set as triggers. I can publish further details if needed.
public class Enemy01 : MonoBehaviour {
public float timeBetweenAttacks = 0.5f;
public int attackDamage = 10;
GameObject player;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
float timer;
void Awake()
{
player = GameObject.FindGameObjectWithTag("Player");
playerHealth = player.GetComponent<PlayerHealth>();
enemyHealth = GetComponent<EnemyHealth>();
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
}
void OnTriggerEnter(Collider other)
{
if (timer >= timeBetweenAttacks && playerHealth.currentHealth > 0)
{
Attack();
}
}
void Attack()
{
timer = 0f;
if (playerHealth.currentHealth > 0)
{
if (gameObject.CompareTag("Player"))
{
playerHealth.TakeDamage(attackDamage);
}
}
}
}
This is a bit too little information to give you a clear answer, you should show us what does your TakeDamage() do to check if the problem isn't there.
Is this what you need? The damage does work outside of that first series of checks for collision.
public class PlayerHealth : $$anonymous$$onoBehaviour {
public int startingHealth = 100;
public int currentHealth;
public float flashSpeed = 5f;
public Image damageImage;
public Color flashColor = new Color(1f, 0f, 0f, 0.1f);
public Sprite player;
Player$$anonymous$$ovement player$$anonymous$$ovement;
bool isDead;
bool damaged;
// Use this for initialization
void Start() {
}
void Awake()
{
player$$anonymous$$ovement = GetComponent<Player$$anonymous$$ovement>();
currentHealth = startingHealth;
}
// Update is called once per frame
void Update() {
if (damaged)
{
damageImage.color = flashColor;
}
else
{
damageImage.color = Color.Lerp(damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
}
damaged = false;
}
public void TakeDamage(int amount)
{
damaged = true;
currentHealth -= amount;
if (currentHealth <= 0 && !isDead)
{
Death();
}
}
void Death()
{
isDead = true;
player$$anonymous$$ovement.enabled = false;
}
}
Next time you should post it as a comment and not as answer :p But yeah this script looks fine, you should check if the attack() is called by by putting something llike print("hit"); inside it, if it won't print "hit" then there has to be a problem with if (timer >= timeBetweenAttacks && playerHealth.currentHealth > 0)
Sorry, I didn't know about that.
Also, I just did your check and it did not print Hit so that line of code is the problem. I tried removing both conditions one at a time to see which one caused the problem but it still did not print.
you need ontrigger 2d you don't have 2D scripting correct this and should work
Answer by CupOfMayo · Oct 07, 2016 at 09:32 PM
Oh wow I'm so blind, if you're working with 2d rigidbodies and 2d colliders OnTriggerEnter won't work. You must use OnTriggerEnter2D(Collider2D other)
as shown here https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html
Thank you very much now it works and the player will take damage so long as I don't run this check. Is there any way I can tell what the problem here is?
if (gameObject.CompareTag("Player"))
{
playerHealth.TakeDamage(attackDamage);
}
Edit: fixed it.
if (gameObject.CompareTag("Enemy01"))
I don't really use CompareTag but I'm pretty sure it is because gameObject.CompareTag("Player")
is checking if the object that the script is attached to is tagged as "Player". I would do if(other.transform.tag == "Player")
Thanks for the info will be sure to update that and note it for the future.
Your answer
Follow this Question
Related Questions
C# 2D top down shooter game, How do I get the player to shoot towards the mouse cursor? 0 Answers
2D-Sprite Bug? 1 Answer
How do I get my character to go the the next level? 1 Answer
OnTriggerStay2D skipped every 25th frame? -- collider turns light green 1 Answer
How to cancel the force caused by collision? So the player is not pushed away when it hits a corner? 0 Answers