Possible Alternative approach to pickups?

To avoid the issue of the pickup being equipped no matter how far it is from the Player, what if instead of having HandleRaycast(PlayerController callingController) in WeaponPickup.cs call the Pickup() function, it simply calls the Mover component of the callingController? Then, you just let the OnTriggerEnter() do its thing and call the Pickup() function when the player enters the trigger radius. To make this work, you also have to include using using RPG.Movement; in WeaponPickup.cs which may not be very elegant from a dependency perspective.

That said, this is exactly what I did and now the player moves to the pickup before equipping it which feels more natural. My implementation changes:
public bool HandleRaycast(PlayerController callingController)
{
if (Input.GetMouseButtonDown(0))
{
Pickup(callingController.GetComponent());
}
return true;
}

to:

    public bool HandleRaycast(PlayerController callingController)
    {
        if (Input.GetMouseButtonDown(0))
        {
            callingController.GetComponent<Mover>().StartMoveAction(transform.position, 1f);
        }
        return true;
    }

Anyone see any issues with this approach from a dependency or other perspective?

1 Like

This is a good solution! Well done!

I absolutely loved this idea but my only problem was the dependency that this created so in order to correct this I went into the fighter script and looked for where we move in the fighter script considering we already have movement used there.

So basically in fighter I did this:

private void Update()
        {
            timeSinceLastAttack += Time.deltaTime;

            if (target == null) return;
            if (target.IsDead()) return;

            if (!GetIsInRange())
            {
                MoveToPosition(target.transform.position);
            }
            else
            {
                GetComponent<Mover>().Cancel();
                AttackBehaviour();
            }
        }

        public void MoveToPosition(Vector3 movePosition)
        {
            GetComponent<Mover>().MoveTo(movePosition, 1f);
        }

With that method extracted I could then go back to the Weapon pickup script and use the already established namespace:

public bool HandleRaycast(PlayerController callingController)
        {
            
            if (Input.GetMouseButtonDown(0))
            {
                callingController.GetComponent<Fighter>().MoveToPosition(gameObject.transform.position);
            }
            return true;
        }

If anyone has an improvement to this I would love to see it. One of the issues I see as of the moment is that you can not cancel the combat by clicking on an Item to move to it.

Thanks guys,
Jason P.

1 Like

I created a component called Collector which implements the IAction interface.

The HandleRayCast() of the Pickup instructs Collector to pickup the object StartCollectPickup(), exactly how CombatTarget tells Fighter to attack an enemy. (This cancels combat!)

In Update() if the target is out or range, move to the item, and if it’s in range, pick it up.

2 Likes

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

Privacy & Terms