Issues with moving and attacking

I have a weird error

In fighter script I use

using UnityEngine;
using RPG.movement;
using RPG.core;

namespace RPG.combat
{
    public class Fighter : MonoBehaviour, IAction // need cancel
    {
        Transform target;
        float timesincelastattack = 0f;
        [SerializeField] float Weaponrange = 2f;
        [SerializeField] float timebetweenattacks = 1f;

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

            if (target == null) return;
         
            if (!GetinRange())
            {
                GetComponent<Mover>().MoveTo(target.position);
             
            }
            else
            {
                GetComponent<Mover>().Cancel();
                //Cancel();
                AttackBehaviour();
                //print("fight not moving");
            }
            
        }

        private void AttackBehaviour() // attack animation
        {
            if (timesincelastattack > timebetweenattacks)
            {
                timesincelastattack = 0;
                GetComponent<Animator>().SetTrigger("attack");
            }
        }
      
       
        bool GetinRange()
        {
            return Vector3.Distance(transform.position, target.position) <= Weaponrange;
        }


        public void Attack(CombatTarget ctarget)
        {   
            GetComponent<ActionScheduler>().StartAction(this);
           // print("attack moving");
            target = ctarget.transform;
        }

        public void Cancel()
        {
            target = null;
           // print("stop attacking (cancel)");
        }

        void Hit() // attack animation state calls this 
        {
            print("hit called");
            if (target == null) return;
            Health health = target.GetComponent<Health>();
            health.TakeDamage(5);
        }
    }
}

The player keeps attacking the enemy prefab (original or now prefab) and refuses to stop (as in I can’t move away) (I have an amusing video I’ll upload if needed).

The only way I can stop it is by calling the cancel function to set the target to null. However enemy doesn’t take damage then!

As it happens, Rick noticed this same behaviour, and we address it in the next lecture. :slight_smile:

Yes watched that vid now. It’s not him continuing to attack after he dies. To be clear my char moves around the screen fine. However, when I click on the enemy he stays in the attack phase. I can’t click to move away until the enemy dies.

using UnityEngine;
using RPG.movement;
using RPG.combat;

namespace RPG.control
{
    public class PlayerController : MonoBehaviour
    {
        Ray lastray;

        // Update is called once per frame
        void Update()
        {
            if (DoCombat()) return; // if we are in combat then no movement.
            if (DoMovement()) return;
        }

        private bool DoCombat()
        {
            RaycastHit[] hits = Physics.RaycastAll(GetmouseRay());
            foreach (RaycastHit hit in hits )
            {
               CombatTarget target = hit.transform.GetComponent<CombatTarget>();
                
               if (target == null) continue; // continue means ignore rest of code in this loop and goto next target in array

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

        private bool DoMovement()
        {
            RaycastHit hit;
            bool hasHit = Physics.Raycast(GetmouseRay(), out hit);
            if (hasHit)
            {
                if (Input.GetMouseButton(0))
                {
                    GetComponent<Mover>().StartToMove(hit.point);
                } 
               // print("in Movement loop DoMovement");
                return true;
            }
            //print("stoped moving");
            return false;
        }
       
        private static Ray GetmouseRay()
        {
            return Camera.main.ScreenPointToRay(Input.mousePosition);
        }
    }
}

I suspect the issue is in fighter class or PlayerController class but for life of me can’t see it.
I have got ActionScheduler and IAction implemented and seeming to work. Yes I had a bug where I made Cancel function in private not public I fix that recently. However the bug is my char get stuck in attack phase and won’t move away (till enemy dies. I did stopattack animation in Cancel fuction after next vid).

Ah, it sounds like your ActionScheduler may not be saving the value of your current action. Paste in your ActionScheduler and we’ll take a look.

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

namespace RPG.core
{
    // this is to manage what we do either combat or movement. 
    public class ActionScheduler : MonoBehaviour
    {
        IAction Currentaction;
        public void StartAction(IAction Action)
        {
            if (Currentaction == Action) return;

            if (Currentaction != null)
            {
                Currentaction.Cancel();
            }
            
           // Currentaction = Action;
        }
    }
}

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

namespace RPG.core
{
    // new one on me need to research this interface, its not a class
    public interface IAction 
    {
        void Cancel(); 
        // all classes that link with this must have a cancel function (or what ever I label this as)
        // Actionsheduler for a start uses IAction
    }
}


Thats my ActionScheduler and Iaction Scripts

Im a muppet I commented out the curentaction = Action line
Don’t know when that happened. I just edited it back after watching
Dependency Inversion With Interfaces vid again lol
I think that fixed it, well it seems to have.

That’s what I suspected the issue was.

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

Privacy & Terms