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 Midnight17 · Oct 07, 2016 at 07:52 PM · 2d gamesprites2d-physicshealth-deduction

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);
             }
             
         }
     }
 }
Comment
Add comment · Show 5
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 CupOfMayo · Oct 07, 2016 at 08:39 PM 0
Share

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.

avatar image Midnight17 · Oct 07, 2016 at 08:47 PM 0
Share

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;
     }
 }
avatar image CupOfMayo Midnight17 · Oct 07, 2016 at 09:01 PM 1
Share

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)

avatar image Midnight17 CupOfMayo · Oct 07, 2016 at 09:28 PM 0
Share

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.

avatar image sid4 · Oct 08, 2016 at 06:11 PM 0
Share

you need ontrigger 2d you don't have 2D scripting correct this and should work

1 Reply

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

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

Comment
Add comment · Show 4 · 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 Midnight17 · Oct 07, 2016 at 10:45 PM 0
Share

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"))

avatar image CupOfMayo Midnight17 · Oct 07, 2016 at 10:54 PM 1
Share

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")

avatar image Midnight17 CupOfMayo · Oct 08, 2016 at 08:53 AM 0
Share

Thanks for the info will be sure to update that and note it for the future.

Show more comments

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

82 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

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


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