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 Lund259 · Nov 30, 2016 at 12:06 PM · movementtriggersdetectionattack

OnTriggerEnter2D only triggers once, until the character or enemy moves again. (With video showcase)

So i have this weird problem, where i can only hit an enemy once until either the enemy or the character moves again. Here is my hurtEnemy script, attached to the "swing animation" of the sword:

 using UnityEngine;
 using System.Collections;
 
 public class HurtEnemy : MonoBehaviour {
 
 
     public int baseDamage;
     private int damageToGive;
 
     public GameObject damageBurst;
 
     public Transform hitPoint;
 
     public GameObject damageNumber;
 
     private PlayerStats thePS;
 
 
 
     // Use this for initialization
     void Start () {
         thePS = FindObjectOfType<PlayerStats>();
     }
     
     // Update is called once per frame
     void Update () {
 
     }
 
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.tag == "Enemy")
         {
             damageToGive = baseDamage + thePS.currentAttack;
 
 
             other.gameObject.GetComponent<EnemyHealthManager>().HurtEnemy(damageToGive);
             Instantiate(damageBurst, hitPoint.position, hitPoint.rotation);
             var clone = (GameObject)Instantiate(damageNumber, hitPoint.position, Quaternion.Euler(Vector3.zero));
             clone.GetComponent<FloatingNumbers>().damageNumber = damageToGive;
             
         }
 
         Debug.Log("Trigger activated");
         
     }
 
 
 
     
 }
 

If i disable my enemyMovement script, and walk up to an enemy i can hit it once, and it works perfect. But if i try to hit it again, the animation fires, but the OnTriggerEnter2D doesn't detect anything. If i then take 1 step, and it doesn't matter if it is closer, furthere away or whatever, then i am able to hit the enemy again. The same thing happens if the enemy moves. How can this be? The swing animation, which the hurtEnemy script and the triggercollider is on, is disabled and only enabled when the player is attacking.

At the bottom of this post, i have also posted a youtube video where i showcase the problem in 10 seconds. If it is to any use, here is my playerController script:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
 
 
     public float moveSpeed;
     private float currentMoveSpeed;
     public float diagonalMoveModifier;
 
     private Animator anim;
     private Rigidbody2D myRigidbody;
 
     private bool playerMoving;
     private Vector2 lastMove;
 
     private static bool PlayerExists;
 
     private bool attacking;
     public float attackTime;
     private float attackTimeCounter;
 
     public string startPoint;
 
     public bool canMove;
 
     private GameManager GM;
 
     // Use this for initialization
     void Start () {
         GM = FindObjectOfType<GameManager>();
         canMove = true;
 
         anim = GetComponent<Animator>();
         myRigidbody = GetComponent<Rigidbody2D>();
         
 
         if (!PlayerExists)
         {
             PlayerExists = true;
             DontDestroyOnLoad(transform.gameObject);
         }
         else
         {
             Destroy(gameObject);
         }
 
         
     }
     
     // Update is called once per frame
     void Update () {
 
         if (Input.GetKeyDown(KeyCode.Escape))
         {
             if(GM.STATE == GameManager.GameState.Playing)
             {
                 
                 GM.changeState(GameManager.GameState.PauseMenu);
             }
             else if(GM.STATE == GameManager.GameState.PauseMenu)
             {
                 
                 GM.changeState(GameManager.GameState.Playing);
             }
 
             
         }
 
         playerMoving = false;
 
         if (canMove)
         {
             if (!attacking)
             {
 
                 //movement
 
                 //horizontal
                 if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
                 {
 
                     myRigidbody.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * currentMoveSpeed, myRigidbody.velocity.y);
                     playerMoving = true;
                     lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0);
                 }
 
                 //vertical
                 if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
                 {
 
                     myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, Input.GetAxisRaw("Vertical") * currentMoveSpeed);
 
                     playerMoving = true;
                     lastMove = new Vector2(0, Input.GetAxisRaw("Vertical"));
                 }
 
                 if (Input.GetAxisRaw("Horizontal") < 0.5f && Input.GetAxisRaw("Horizontal") > -0.5f)
                 {
                     myRigidbody.velocity = new Vector2(0f, myRigidbody.velocity.y);
                 }
 
                 if (Input.GetAxisRaw("Vertical") < 0.5f && Input.GetAxisRaw("Vertical") > -0.5f)
                 {
                     myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, 0f);
                 }
 
                 if (Input.GetKeyDown(KeyCode.Space))
                 {
                     attackTimeCounter = attackTime;
                     attacking = true;
                     myRigidbody.velocity = Vector2.zero;
                     anim.SetBool("Attack", true);
                 }
 
                 if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0.5f && Input.GetAxisRaw("Vertical") > 0.5f)
                 {
                     currentMoveSpeed = moveSpeed * diagonalMoveModifier;
                 }
                 else
                 {
                     currentMoveSpeed = moveSpeed;
                 }
 
             }
 
             if (attackTimeCounter > 0)
             {
                 attackTimeCounter -= Time.deltaTime;
             }
 
             if (attackTimeCounter <= 0)
             {
                 attacking = false;
                 anim.SetBool("Attack", false);
 
             }
         }
         else
         {
             myRigidbody.velocity = Vector2.zero;
         }
             anim.SetBool("PlayerMoving", playerMoving);
             anim.SetFloat("LastMoveX", lastMove.x);
             anim.SetFloat("LastMoveY", lastMove.y);
 
             anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
             anim.SetFloat("MoveY", Input.GetAxisRaw("Vertical"));
 
         
 
 
         
 
 
 
     }
 }
 

I have been trying to figure this out, but i just don't know why it shouldn't work. Is it a unity bug? Or do i have som logical error somewhere?

Here is a video of me showcasing the problem (only 10 seconds long): RPG attack problem video

im using Unity 5.4.1f1

Thanks for your time!

EDIT: I've tried fooling around with using OnTriggerStay instead and using some sort of counter, and it has the exact same result! It only fires the OnTriggerStay, if i attack max 1 sec after my last movement.

EDIT2; updating to unity 5.5.0f3 didn't help. This is driving me nuts

I discovered, that the trigger only works, when swing big is activated while the character is moving or has been moving recently (like in the past second). The swingBig OnTriggerEnter will never activate, while standing still for more than a sec.

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

1 Reply

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

Answer by Lund259 · Dec 01, 2016 at 02:30 PM

I just solved the issue. Just writing it here if someone else are experiencing the same issue. All i did was i added a rigidbody2D to my swingBig animation, and checked IsKinematic. Thats is. Can't believe this issue bugged me for days lol!

Comment
Add comment · 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

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

91 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity C# - How to check if random moving gameobject is within boundaries 0 Answers

Find Objects in Trigger and Effect Player 1 Answer

Move and Attack ( for MOBA style minions) 1 Answer

Help 2D attack 1 Answer

My enemy runs backwards away from my player object instead of towards it. 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