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 Merrick20 · Oct 07, 2016 at 08:08 PM · linerendererloadlevelcolission

How to avoid my player being killed twice

Hi Everyone,

I've been trying to get my head around something that's happening in my game. It's a kind of platformer. And the thing is that a specifical type of enemy attack is killing my player twice.

This is the script that takes damage:

     void Update()
     {
         if (immune) 
         {
             if (immuneTimer > 0)
             {
                 immuneTimer -= Time.deltaTime;
                 
             }else{
                 immune = false;
                 immuneTimer = 2f;
                 
             }
         }
     }
 
     void Start()
     {
         rend = GetComponent<Renderer>();
         currentHP = hp;
         //immuneTimer = immunityTime;
     }
 
     void OnTriggerEnter2D (Collider2D col)
     {    
         if (!immune)
             {
         DamageSource damageGiver = col.GetComponent<DamageSource> ();
 
         if (damageGiver) {
 
             if (this.tag == "Enemy")
             {
                 SendMessageUpwards ("Defend", SendMessageOptions.DontRequireReceiver);
             }
         
 
             foreach (DamageType typeOfDamage in damageGiver.damageTypeDealt) {
 
                 if (!immuneTo.Contains (typeOfDamage)) {
 
                     if (weakness.Contains (typeOfDamage))
                         {
                             currentHP -= damageGiver.damageDealt *2;                    
 
                     }else{
                         currentHP -= damageGiver.damageDealt;}
 
                     StartCoroutine (CheckLife ());
 
 
                     
                     if (this.tag == "Player")
                     {
 
                         var player = this.GetComponent<PlayerMovement>();
                         EventManager.BroadcastUnderAttack();
                         
                         player.knockbackCount = player.knockbackTimer;
 
                         immune = true;
                         
                             //Knockback
                         if(col.transform.position.x > player.transform.position.x)
                         {
                             player.knockFromRight = true;
                         }else{
                             player.knockFromRight = false;
                             }
                         }
                     }
                 }
             }        
         }
     }
     IEnumerator CheckLife()
     {
             rend.material.color = Color.red;
 
         yield return new WaitForSeconds (0.25f);
 
             if (currentHP <= 0) 
         {
             if (this.tag == "Enemy")
             {
             int dropAmount = Random.Range (0, 100);
             //print (dropAmount);
             if (dropAmount <= chanceToDrop) {
                 Instantiate (manaSource, this.transform.position, Quaternion.Euler (new Vector3 (0, 0, 0f)));
                 }
             }
             Destroy(this.gameObject);
             if (this.tag == "Player") 
             {
                 EventManager.BroadcastPlayerKIA();
             }
         }
             yield return new WaitForSeconds (0.25f);
             rend.material.color = Color.white;
 
         yield break;
     }


As in the game you have more than one type of damage, it checks on the damage Dealer what kind of damage is doing to it, and then hurts the player. The Player gets immunity for a couple of seconds and if the player gets destroyd it sends an event about it and then comes this script

 void HandleOnPlayerKIA ()
     {
         //print ("Lifes Remaining" + myStats.lifesRemaining);
         myStats.lifesRemaining --;
         myStats.UpdateTexts ();
         if (myStats.lifesRemaining <= 0) {
             LoadLevel ("TitleScreen");
             myStats.lifesRemaining = 2;
         } else {
             LoadLevel (levelLoaded);
         }
     }

This script substract a life from the player and then if there are no more life remaining, it goes to TitleScreen or else reloads the where the player is.

This has been working for several enemys and levels, but then a boss appear, which shoots a laser beam. Which is a line renderer component with this scritp on it

     private LineRenderer laser;
     private BoxCollider2D box;
     private float counter;
     private float distance;
 
     public Transform origin;
     public Transform destination;
 
     public float lineWidth = 2;
 
     public float lineDrawSpeed = 6f;
 
     public bool shooting;
 
     // Use this for initialization
     void Start () {
 
         box = gameObject.AddComponent <BoxCollider2D>() as BoxCollider2D;
         box.isTrigger = true;
         box.enabled = false;

         laser = GetComponent<LineRenderer> ();
         laser.SetPosition (0, origin.position);
         laser.SetWidth (2.0f, 2.0f);
 
         distance = Vector3.Distance (origin.position, destination.position);
 
         EventManager.OnPlayerKIA += HandleOnPlayerKIA;
     
     }
     
 
     void Update () {
 
         if (shooting) {
             box.enabled = true;
             laser.enabled = true;
 
             if (counter < distance) {
                 counter += .1f / lineDrawSpeed;
 
                 float x = Mathf.Lerp (0, distance, counter);
 
                 Vector3 pointA = origin.position;
                 Vector3 pointB = destination.position;
 
                 Vector3 pointAlongLine = x * Vector3.Normalize (pointB - pointA) + pointA;
 
                 laser.SetPosition (1, pointAlongLine);
 
                 box.transform.position = origin.position +(destination.position - origin.position)/ 2;
                 //box.transform.LookAt (origin.position);
                 box.size = new Vector2 ((destination.position - origin.position).magnitude, lineWidth);
             }
         } else {
             box.enabled = false;
             laser.enabled = false;
         }
     
     }
 
 

What it does is, when the line renderer is activated it also expands a box collider component to check if the player get's in the way. And then has a class called DamageSource which interacts with Damage Receiver.

So, to sum it up. The player gets killed twice by this enemy. That shouldn't happen. Any ideas? Help!? Been struggling with this for a couple of days by now, and don't know what to do.

I tried destroying the laser whean OnPlayerKIA, but didn't work. Tried to disable the laser component OnTriggerEnter if other collider's tag == "Player"... so far nothing worked...

Thanks in advance.

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

· Add your reply
  • Sort: 

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

75 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

Related Questions

How to add a fading laser effect to my laser gun ? Unable to fade out the currently used line renderer over time. 1 Answer

Any way to save a trail renderer? 1 Answer

Why does line renderer color is gray with 255, 255, 255 RGB? 0 Answers

Can't get a smooth following line 1 Answer

Line Renderer how to move individual keys 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