I'm creating a 2D game and spawn my enemies with Collider 2D and Rigidbody2D, and my hero is attacking them, but he casts his attacking ray in strange directions (but mostly if works ok for moving enemies), but it doesn't see static enemies at all! Like I literally can't kill them when I'm in front of them. I guess the problem is in the direction Vector, but I have no idea what I should change

Collider2D enemy= Physics2D.CircleCast(this.transform.position, 2, new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")), 3).collider;
 if (enemy.CompareTag("Enemy"))
                if (enemy.GetComponent<MoveToWayPoints>() != null) // the check of moving enemy
                    enemy.GetComponent<MoveToWayPoints>().hp.GetComponent<HP>().Dmg(10, 1);
                if (enemy.GetComponent<ShamanScript>() != null) // the check of static shaman enemy
                    enemy.GetComponent<ShamanScript>().hp.GetComponent<HP>().Dmg(10, 1);
  • \$\begingroup\$ Can you confirm that you are continuing to hold the analog stick (or WASD keys) in the direction of the static enemy over the whole time interval in which this code should run? (ie. you don't walk up to the static enemy, release the stick/WASD keys to stop, then attack?) If so, can you edit your question to include a Minimal Complete Verifiable Example? We'll need to see how the working and non-working enemies as well as your player object are set up in the hierarchy and inspector, so we can reproduce the same situation in a new, empty project. \$\endgroup\$
    – DMGregory
    Mar 3 at 20:43
  • \$\begingroup\$ Can you explain MoveToWayPoint and ShamanScript? Also, is that hp acts the same in both of the components? \$\endgroup\$
    – Arian_ki
    Mar 3 at 21:08
  • \$\begingroup\$ @DMGregory yes, I'm using WASD and trying to hit from all sides. And I forget to say, moving enemies are attackable, but when they are lose their speed from code, they're not see my attacks \$\endgroup\$
    – Chamistery
    Mar 6 at 17:37
  • \$\begingroup\$ @ariankeshvari MoveToWayPoint uped to every spawned enemy. It gives them a point where to go, methods how to give a damage and to stop before it and how to be destroyed. Also there is hp object to every enemy, which decreases. ShamanScript is a script for static enemy shaman, which slow down my hero. It also has hp component. \$\endgroup\$
    – Chamistery
    Mar 6 at 18:00
  • \$\begingroup\$ So what's the difference between the hp components on the two scripts? I would say create a hp script to use for every enemy in the game, it's really time-saving. Also, try using TryGetComponent instead of null check \$\endgroup\$
    – Arian_ki
    Mar 6 at 18:38


You must log in to answer this question.

Browse other questions tagged .