Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Attack Script makes me respawn?

Discussion in 'Scripting' started by CoopReed, Nov 23, 2015.

  1. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    Hi guys,

    Made this script while following a tutorial, it works fine no compile errors, however when I press the button (f) it basically enables my respawn script, or at least that is what I think.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerAttacking : MonoBehaviour {
    5.  
    6.  
    7.     private bool attacking = false;
    8.  
    9.     private float attackTimer = 0;
    10.     private float attackCd = 0.3f;
    11.  
    12.     public Collider2D attackTrigger;
    13.  
    14.     private Animator anim;
    15.  
    16.  
    17.     void Awake()
    18.     {
    19.         anim = gameObject.GetComponent<Animator>();
    20.         attackTrigger.enabled = false;
    21.     }
    22.     void Update()
    23.     {
    24.         if (Input.GetKeyDown("f") && !attacking)
    25.         {
    26.             attacking = true;
    27.             attackTimer = attackCd;
    28.  
    29.             attackTrigger.enabled = true;
    30.         }
    31.         if (attacking)
    32.         {
    33.             if (attackTimer > 0)
    34.             {
    35.                 attackTimer -= Time.deltaTime;
    36.             }
    37.             else
    38.             {
    39.                 attacking = false;
    40.                 attackTrigger.enabled = false;
    41.             }
    42.         }
    43.     }
    44. }
    45.  
    Thanks guys, any help is very much appreciated!
     
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    What exactly is meant to happen? All it looks like is you hit f & a timer counts down & nothing else happens
     
  3. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    It is ment to play an animation. I have added it to my controleed and then added the attacking parameter to it
     
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Do you have any other scripts because there isn't anything there that will respawn you? Perhaps when you are attacking the enemy is actually damaging & killing you?
     
  5. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    I have another script that rests the level when you hit a collider but this just restarts the level when you press f
     
  6. Kerith

    Kerith

    Joined:
    Jan 24, 2015
    Posts:
    7
    Does the respawn script interacto with the collider? Maybe when you enable it, it automatically collides with something and respawns you.

    Could you post the respawn script?
     
    tedthebug likes this.
  7. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    When you activate the attack the collider may be hitting yourself
     
    Kerith likes this.
  8. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    No, let me elaborate. I press play on my game I spawn. I do not move at all I just stand there and press f, then the game respawns. That is all that is happening. NO idea why
     
  9. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Then your collider for the attack is probably hitting yourself because the code you posted has no way to respawn. You said the level only restarts when the collider hits something so if the player isn't near anything & it resets when you press f then the collider has to be hitting something & logically that can only be the player hitting himself.
    (Now I have a picture of a bully saying "stop hitting yourself" as he uses the players hand to hit himself)
     
    Kerith likes this.
  10. Kerith

    Kerith

    Joined:
    Jan 24, 2015
    Posts:
    7
    I still think that it's the collider. Remember that, when you activate a collider, it activates for all scripts, not just the one activating it.

    Thus, if you have something like
    Code (CSharp):
    1. if (/*collides with something*/) {
    2. respawn();
    3. }
    somewhere else in the code, it will automatically respawn, since it will collide with something, be it a floor, or itself, as @tedthebug said.

    Itwould be helpful if you posted your respawn code, though.