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 /
This question was closed May 25, 2017 at 12:57 PM by NewDevDave for the following reason:

Issue resolved myself

avatar image
0
Question by NewDevDave · May 27, 2017 at 02:27 PM · collisions

I am having an issue with a collision that used to work but now for some reason will not detect. Please help.

I have been working on a 2D platform game and I have a player damage script which links into the relevant animations and reduces health etc. This was working absolutely perfectly until I tried to add a lives system following a tutorial. Since then the final collision to play the death animation simply will not register and I have no idea why. I have removed all the extra scripts and objects that were added and my damage script is now exactly as it was when it was working but the final collision to make health go from 1 to 0 won't work. The trigger in the animator works as I can click it and the animation plays fine and if I script for the Die function to be called automatically this also works but the player dies without needing a collision. I have tried using Debug.Log and this shows that the collision is not detecting but I am completely lost as to why not and would really appreciate any help which could be offered. Below is the script for damage.

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerDamage : MonoBehaviour
 {
 //    private LevelManager levelManager;
     private NewPlayControl player;
     public int health;
     public int currentHealth;
     public int damage = 1;
     public bool BeenDamaged = false;
     public bool IsDead = false;
     public bool knockfromRight;
     public bool knockfromLeft;
     public float knockback;
     public CircleCollider2D enemycollider;
     public CapsuleCollider2D playercollider;
     public Transform rightCheck;
     public Transform leftCheck;
     float hitRadius = 0.1f;
     public LayerMask whatisEnemy;
     public int Lives;
     public int currentLives;
 
     // Use this for initialization
     void Start ()
     {
 //        levelManager = FindObjectOfType<LevelManager>();
         player = FindObjectOfType<NewPlayControl>();
         currentHealth = health;
         playercollider = GameObject.Find("SuperNoCollider").GetComponent<CapsuleCollider2D>();
         enemycollider = GameObject.Find("EnemyGround").GetComponent<CircleCollider2D>();
         currentLives = Lives;
     }
 
     void TakeDamage()
     {
         currentHealth = (currentHealth - damage);
     }
 
     void HitDirection()//This determines which side is colliding with enemy
     {
         knockfromRight = Physics2D.OverlapCircle(rightCheck.position, hitRadius, whatisEnemy);
         knockfromLeft = Physics2D.OverlapCircle(leftCheck.position, hitRadius, whatisEnemy);
     }
 
     IEnumerator OnCollisionEnter2D(Collision2D other)
     {
         if (other.gameObject.tag == "Enemy")
         {
 
             if (player.anim.GetCurrentAnimatorStateInfo(0).IsTag("Attack"))
             {
                 Physics2D.IgnoreCollision(playercollider, enemycollider, true);
             }
             else if (!BeenDamaged && !IsDead && currentHealth > 1)
             {
                 HitDirection();
                 if (knockfromLeft)
                 {
                     player.anim.SetTrigger("LTakeDamage");
                     knockfromLeft = true;
                     player.rb.AddForce(transform.right * knockback);
                     player.rb.AddForce(transform.up * knockback);
                     player.CanControl = false;
                     yield return new WaitForSeconds(1);
                     knockfromLeft = false;
                     player.CanControl = true;
                     TakeDamage();
                     BeenDamaged = true;
                     yield return new WaitForSeconds(3);
                     BeenDamaged = false;
                 }
                 else if (knockfromRight)
                 {
                     player.anim.SetTrigger("RTakeDamage");
                     knockfromRight = true;
                     player.rb.AddForce(transform.right * -knockback);
                     player.rb.AddForce(transform.up * knockback);
                     player.CanControl = false;
                     yield return new WaitForSeconds(1);
                     knockfromRight = false;
                     player.CanControl = true;
                     TakeDamage();
                     BeenDamaged = true;
                     yield return new WaitForSeconds(3);
                     BeenDamaged = false;
                 }
                 else if (!BeenDamaged && !IsDead && currentHealth == 1)
                 {
                     TakeDamage();
                 }
                 if (currentHealth == 0)
                 {
                     Die();
                 }
             }
         }
     }
 
     public void Die()
     {
         BeenDamaged = false;
         IsDead = true;
         player.anim.SetTrigger("Die");
 
         if (IsDead == true)
         {
             player.CanControl = false;
             playercollider.enabled = false;
             player.rb.gravityScale = 0;
          }
     }
 }
 

PS - I have also noticed that the CapsuleCollider2D is being removed from my player when I press play in the editor for some unknown reason and I am not sure if this is the issue as it stills registers the first collisions.][1]

editor.png (23.2 kB)
editor2.png (26.6 kB)
Comment
Add comment
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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Collisions won't work(Please check text) 1 Answer

Colliders for tunnels 0 Answers

Child gameObject with collider not colliding if parent has rigidBody 2 Answers

My camera wont stay above the ground I have in my scene. How to fix? 1 Answer

Network objects don't collide correctly 2 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