Making enemies attack range dynamic in code based on weapon range

Hi I’ve made a few code changes to make it so the enemies attack range will work based on the equipped weapons attack range rather than having to manually increase the enemies chase distance.

First in fighter
move equipweapon into awake otherwise the execution order will cause an error:

 private void Awake()
        {
            _mover = GetComponent<Mover>();
            _animator = GetComponent<Animator>();
            EquipWeapon(_defaultWeapon);
        }

Make a public getter for currentweapon:
public Weapon CurrentlyEquipedWeapon => _currentWeapon;

Now in AIController
create an attackRange float and then set it start:

private void Start()
        {
            _guardPosition = transform.position;
            _attackRange = _fighter.CurrentlyEquipedWeapon.WeaponRange;
        }

Then in the bool that checks if in range of player (I can’t remember original name I changed mine) create an if else that will make the chase/attack range based on if the attack range is higher than the set chase distance:

  private bool InAttackRangeOfPlayer()
        {
            float distanceToPlayer = Vector3.Distance(_player.transform.position, gameObject.transform.position);
            if (_attackRange > _chaseDistance)
            {
                return distanceToPlayer < _attackRange;
            }
            else
            {
                return distanceToPlayer < _chaseDistance;
            }
        }

Well done! We’ll be doing something very similar in a few lectures!

1 Like

I’ve noticed a few times now I keep thinking of solutions to problems that get addressed in later lectures, I should probably wait towards the end and come back and fix anything that isn’t addressed then.

I strongly disagree. While sometimes you may find you need to walk back a solution to a problem you solved before you got to the lesson, the overarching goal of the RPG series isn’t just to teach you to make an RPG (though we hope, by the time you’re through, you’ve got the gist of that), it’s to get you thinking in a more intermediate or even advanced level of thinking through.

A lot of students have taken our courses, but they get out just what they put in. You’re getting more out of the course, IMO, than the average student because you’re taking what we’ve done and started to think through beyond just what we taught. Eventually, you’ll be in a place were you don’t need a course by us to tell you how to solve a particular problem. That ability is born out of just what you did with this post, thinking through a problem and solving it even though in three lectures we’ll be solving it for you.

1 Like

That has put it into a very good perspective, I think you’re definitely correct. Even reflecting back on my own experiences of when I first did one of gamedev.tvs 2d courses when I was complete beginner I just followed a long unable to do many of even the most basic of challenges.

Which I then found whenever I tried to do something outside of the tutorial I realized I had learnt very little. It wasn’t until coming back to some of the courses including this one with more knowledge and doing my own versions of the code that I really started to understand how everything is working.

So I guess I’ll continue solving problems as I have been doing which sometimes makes it harder to follow a long when my code is different, but I definitely learn more which is the point in the end.

Actually, I have a trick for that… whenever I take a course (these days, usually so I’m familiar with the material so I can properly support the students), I have two versions of the course. The first version follows the instructor’s code as closely as possible, and the second version is my optimized version.

1 Like

I plan on doing the same for the RPG courses, I find I learn far more that way too.

Privacy & Terms