Attacking wont stop?

When i test the game/script in unity, it moves to the target and stop with correct range that it is supposed to, start attacking and then when i click to move away it keeps attacking. Also the throttle for my attacks doesn’t affect it, im unsure if i missed something.

Also i have been adding everything addil by hand, when i follow in VSC, i dont have the light bulb and certain commands dont work to prefill for my code.

using UnityEngine;
using RPG.Movement;
using RPG.Core;

namespace RPG.Combat
{
    public class Fighter : MonoBehaviour, IAction
    {
        [SerializeField] float weaponRange = 2f;
        [SerializeField] float timeBetweenAttacks = 1f;
      
        Transform target;

        float timeSinceLastAttack = 0;

        private void Update()  
        {
            timeSinceLastAttack += Time.deltaTime;
            if (target == null) return;
            if (!GetIsInRange())
            {
                GetComponent<Mover>().MoveTo(target.position);
            }
            else 
            {
                GetComponent<Mover>().Cancel();
                AttackBehaviour();
            }
        }
 
       private void AttackBehaviour()
        {
            if (timeSinceLastAttack > timeBetweenAttacks)
            {
                GetComponent<Animator>().SetTrigger("attack");
                timeSinceLastAttack = 0;
            }
        }

        private bool GetIsInRange()
        {
            return Vector3.Distance(transform.position, target.position) <weaponRange;
        }

        public void Attack(CombatTarget combatTarget)
        {
            GetComponent<ActionScheduler>().StartAction(this);
             target = combatTarget.transform;
        }

        public void Cancel()
        {
            target = null;
        }

        //Animation Event
        void Hit() 
        {
            
        }
    }
}

While the animator is still up, instead of in the video it doing the load below locomotion, its doing it constantly below attack. It just keeps attacking and moving at same time.

Incase mover script is needed,

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.AI;

using RPG.Core;

namespace RPG.Movement

{

public class Mover : MonoBehaviour, IAction

{

    [SerializeField] Transform target;

    NavMeshAgent navMeshAgent;

    private void Start() 

    {

        navMeshAgent = GetComponent<NavMeshAgent>();

    }

    void Update()

    {

        UpdateAnimator();

    }

    public void StartMoveAction(Vector3 destination)

    {

        GetComponent<ActionScheduler>().StartAction(this);

        MoveTo(destination);

    }



    public void MoveTo(Vector3 destination)

    {

        navMeshAgent.destination = destination;

        navMeshAgent.isStopped = false;

    }



    public void Cancel() 

    {

        navMeshAgent.isStopped = true;

    }

    private void UpdateAnimator()

    {

        Vector3 velocity = navMeshAgent.velocity;

        Vector3 localVelocity = transform.InverseTransformDirection(velocity);

        float speed = localVelocity.z;

        GetComponent<Animator>().SetFloat("forwardSpeed", speed);

    }  

}

}

For some reason while playing with all the settings, the attack animation exit to locomotion was set to a natural exit time of .688. However when i subtracted the last 8, when i click to move away it doesnt continue to attack and move at the same time. Would this be a bug?

It seems unlikely. You should have two transitions from Attacking to Locomotion. One should be the natural exit with no conditions. The second should be tied to a stopattackng trigger and the “Has Exit Time” should be unchecked.

I just got done setting up the death animations and 2nd transition to stop attacking when i click off, now when i just click for my character to attack, the time between attacks still isn’t affecting it no matter how large i place it. Also if my character attacks 2 or 3 times and i click away to cancel during the attack, my character just keeps punching and moving. The only difference is im using the attack 2 instead of attack 3 in video.

image
i also get a bunch of these while hes still attacking the air and moving

So I gather you’ve gotten the Hit() method populated…
You can eliminate the null reference by starting Hit() with

if(!target) return;

For the two transitions from Attack=>Locomotion, the one with StopAttack should have no exit time. It should look something like this:

The normal transition should have Exit Time checked, and should look something like this:
image

ok so this seems to have fixed it. I removed the attack animation in the flow chart all together and reput it exactly like yours and its fixed. However i could swear it looked identical before i removed it. Im not sure maybe it just didnt register properly. ty a bunch for the help.

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

Privacy & Terms