Character doesn't attack enemy

I solved the previous problem myself after carefully examining my code. Now I can’t get the print command on the console to show up.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace RPG.Combat
{
    public class Fighter : MonoBehaviour
    {
        public void Attack(CombatTarget target)
        {
            print("asdad");
        }
    }
}

This is also fairly simple?

That is very polite of your character. :grinning_face_with_smiling_eyes:

It always feels good when you work it out.

Can you paste the code from the PlayerController()? That is where the Attack method on Fighter is called.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Combat;
using RPG.Movement;


namespace RPG.Control
{
    public class PlayerController : MonoBehaviour
    {
        private void Update()
        {
            if (InteractWithCombat()) return;
            if (InteractWithMovement()) return;
        }

        private bool InteractWithCombat()
        {
            RaycastHit[] hits = Physics.RaycastAll(GetMouseRay());
            foreach (RaycastHit hit in hits)
            {
                CombatTarget target = hit.transform.GetComponent<CombatTarget>();
                if (target == null) continue;

                if (Input.GetMouseButtonDown(0))
                {
                    GetComponent<Fighter>().Attack(target);
                }
                return true;
            }
            return false;
        }

        private bool InteractWithMovement()
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            bool hasHit = Physics.Raycast(ray, out hit);
            if (hasHit)
            {
                if (Input.GetMouseButtonDown(0))
                {
                    GetComponent<Mover>().StartMoveAction(hit.point);
                }
                return true;
            }
            return false;
        }

        private static Ray GetMouseRay()
        {
            return Camera.main.ScreenPointToRay(Input.mousePosition);
        }
    }
}

I think I fixed it by assigning Combat Target scripts to Player and Enemy.

So programming is all about solving problems like this right? That’s what all the courses tell me (this is not my first course, I just ask a lot of questions and I’m not operating well because of my cold)

Yes, that’s right. However, we can solve many more problems with programming, not just problems “like this”. If you decide to solve other problems in the future, maybe with another programming language or another framework, you’ll very likely encounter similar problems, similar concepts and similar solutions. That’s why it is crucial to develop problem solving skills by solving problems. Keep it up! :slight_smile:


See also:

1 Like

Going to add to Nina.

Programming is about solving problems. If you can’t solve problems yourself, your usefulness as a programmer will be limited.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms