Brian would you able to post your Collector example

Hi Brian,

Would you be able to post your Collector example so that we still move over (or at least to) the objects we collect? I attempted based on your comment, but I had trouble implementing. Thanks.

I got it! Thanks for the idea. I spent a bit more time on it.

  1. Created C# script Collector, added to Player prefab
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Core;
using RPG.Movement;

namespace RPG.Combat
{
    public class Collector : MonoBehaviour, IAction
    {

        private WeaponPickup target = null;

        private void Update()
        {
            if (target == null) { return; }

            if (!GetIsInRange())
            {
                Debug.Log("!GetIsInRange was called");
                MoveToPosition(target.transform.position);
            }
            else
            {
                Debug.Log("GetIsInRange was called");
                GetComponent<Mover>().Cancel();
                CollectBehaviour();
            }
        }

        public void MoveToPosition(Vector3 movePosition)
        {
            Debug.Log("MoveToPosition was called");
            GetComponent<Mover>().MoveTo(movePosition, 1f);
        }


        public void Collect(GameObject collectTarget)
        {
            Debug.Log("Collect was called");
            GetComponent<ActionScheduler>().StartAction(this);
            target = collectTarget.GetComponent<WeaponPickup>();
        }

        private bool GetIsInRange()
        {
            Debug.Log("GetIsInRange was called");

            return Vector3.Distance(transform.position, target.transform.position) < 1f;
        }

        private void CollectBehaviour()
        {
            Debug.Log("CollectBehaviour was called");
            transform.LookAt(target.transform);
        }


        #region IAction Interface
        public void Cancel()
        {
            Debug.Log("Cancel was called");
            StopAttack();
            target = null;
            GetComponent<Mover>().Cancel();
        }

        private void StopAttack()
        {
            Debug.Log("StopAttack was called");
            GetComponent<Animator>().ResetTrigger("attack");
            GetComponent<Animator>().SetTrigger("stopAttack");
        }
        #endregion
    }

}
  1. Changed WeaponPickup HandleRaycast to;
 public bool HandleRaycast(PlayerController callingController)
        {
            if (Input.GetMouseButton(0))
            {
                callingController.GetComponent<Collector>().Collect(gameObject);
            }
            return true;
        }
2 Likes

Well done! This is essentially what I would have posted as example code!

You can use this pattern for any location based action (in Quests and Dialogues, you can use this to walk to the speaker, in the soon coming course Shops and Abilities, you can use this to walk to the shop keeper).

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

Privacy & Terms