I have a character who can move and jump, problems arose when I tried to teach him to attack. I attached an object with Circle.Collider2D to the character and tried to make the character attack by pressing the LMB and deal damage if there is an enemy in the object area.

Character Script:

 using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerHit : MonoBehaviour { public bool canHit; public static int damage; public EnemyHp enemyhp; void Start() { damage = 10; } private void Awake() { } void Update() { if (Input.GetMouseButtonDown(0)) { if (canHit) { enemyhp.takeDamage(damage); } } } private void OnTriggerEnter2D(Collider2D collision) { canHit = true; } private void OnTriggerExit2D(Collider2D collision) { canHit = false; } } 

The script object on the character:

 using System.Collections; using System.Collections.Generic; using UnityEngine; public class RotateCircle : MonoBehaviour { public CircleCollider2D circle; void Start() { } private void Awake() { circle.GetComponentInChildren<CircleCollider2D>(); } void Update() { if (Input.GetKey(KeyCode.A)){ circle.transform.rotation = Quaternion.Euler(0, 180, 0); } if (Input.GetKey(KeyCode.D)) { circle.transform.rotation = Quaternion.Euler(0, 0, 0); } } } 

The enemy's script:

 using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyHp : MonoBehaviour { public int maxHealth = 100; public int curHealth = 100; public BoxCollider2D box; void Start() { } private void Awake() { box.GetComponent<BoxCollider2D>(); } void Update() { } public void takeDamage(int damage) { curHealth -= damage; if (curHealth <= 0) { curHealth = 0; Debug.Log("Dead"); Destroy(gameObject); } Debug.Log(curHealth); } } 

I need the character to attack all enemies within Circle.Collider2D when pressing the LMB. Give me an example of the code, or explain what I need to do in order to realize my plans.

As I understand it, all my difficulties are due to a poor understanding of c #. Tell me, please, books / courses for people with knowledge of this language at the level "just above the syntax".

2 answers 2

Hang a separate collider with a sword Weapon tag. On the enemy on call OnCollisionEnter() check the object tag and take away life.

There is a similar question here, just about firearms. But you have the same approach:

Unity3D access to the same int variables in different scripts, provided that each gameObject has only 1 script

  • Bad answer. First, why tags, if you can correct the collision matrix, which will speed up the physics engine and allow you not to burden the CPU with unnecessary checks. Secondly, why bother to hang a collider somewhere, if the key event here is pressing a button? Extra resources that do not present anything fundamentally unique. - RiotBr3aker

1) What is it and what do you expect from it?

 private void Awake() { box.GetComponent<BoxCollider2D>(); } 

GetComponent , as the name implies, returns a reference to the component. If this link is not saved anywhere, then what is the point in calling such a method?


2) OverlapCircleAll / OverlapBoxAll

Since there is no physics-driven combat system here, a permanent collider is not needed for weapons

Get a list of all colliders that fall within a circular area.

In general, there was hardly a goal in the circle, usually objects are approximated using rectangles, for which we will need the OverlapBoxAll method:

Get a list of all colliders that fall within a box area.


For example, I will give my simplified classes.

Enemy:

 public class Enemy : MonoBehaviour { public float health = 10f; public void AbsorbDamage(float damage) { if((health -= damage) <= 0) { Destroy(gameObject); } } } 

Player:

 public class Attack : MonoBehaviour { public Vector2 attackRange = new Vector2(2f, 2f); public float attackPower = 3f; // от "оружия" и работаем public Transform weapon; void Update () { if (Input.GetMouseButtonDown(0)) { // с помощью первых двух параметров можно задать // любой прямоугольник где угодно foreach (Collider2D col in Physics2D.OverlapBoxAll(weapon.position, attackRange, 0f)) { col.GetComponent<Enemy>().AbsorbDamage(attackPower); } } } } 

Data OverlapXXX methods can work even with a custom collider and can be used in many places.