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".