Attacking Stopped All Together

So after I had one issue with the ActionScheduler, that was fixed here. I followed the tutorial step by step line by line. The only thing I did differently was the character color. I gave my character a red color and changed the enemy to have the blue one so it was easier to see them and to give my character a different appearance. Somehow after that change. When I go to attack the attack animation will not trigger, none of my triggers activate and it is as if the player dose not recognize the enemy is clicked on.

I do not think changing the enemy color would cause the attacking to stop all together.

In case I ma actually missing something here is my code if needed.

If you need to look at further code or other information please let me know.

The Hit() method is updated in my current code. just might not be in the link above.
// Animation Event
void Hit()
{

        if (target == null)
        {
            return;
        }
        target.TakeDamage(weaponDamage);
    }

For future code pastes, please paste them in here, rather than using pastemyst. It will make my job much easier.
Before pasting the code, type three backwards quotes on their own line (the key next to the 1 on your keyboard) and after the paste, three more backwards quotes on their own line.
For example:
```
void Update()
{
DoSomethingInteresting();
}
```
becomes

void Update()
{
    DoSomethngInteresting();
}

When you say you changed the “color” of the characters, was this editing/modifying the materials, or dragging a different character into the Prefab? In the case of the latter, you need to make sure that the avatar in the animator matches the character that you’ve dragged into the inspector.

Also make sure that your enemies still have a CombatTarget, that your player has a PlayerController, and that all of your characters still have Health, Fighter, Animator, , and ActionScheduler.

Thank you for letting me know about sending my code in the messages. I will make sure to do so from now on.

To answer the second question, I am referring to going into materials and dragging a color option for the enemy and player to give the enemy a blue look and my player the red look. I did not change what prefab was being used for the player or the enemy.

That should affect nothing at all.

Are you able to move the character around?

Sometimes (and I cannot nail down why this happens on some systems and not others), using GetMouseButton() in Mover resets the action even when the Fighter has been detected. Let’s put a “click trap” in PlayerController to prevent this.

First, you’ll need a bool to track whether or not we are in a clicked state

bool clickedOnComponent;

Then in InteractWithCombat() in the if(GetMouseButtonDown(0)) block:

clickedOnComponent = true;

Now in InteractWithMovement() change the if statement to read:

if (Input.GetMouseButton(0) && !clickedOnComponent)

and finally, start Update() with

if(Input.GetMouseButtonUp(0)) clickedOnComponent=false;

Yes I am able to move around. I can click the left mouse button as well as holding down the left mouse button to move. I added the code into my PlayerController() script, I tested and I can still move the same as before but my character still is not able to click on the enemy and react correctly.

Before and after the code addition when I try to click on the enemy, the player runs up to the enemy but will not activate any of my triggers. The player just runs into the enemy and pushes through the enemy to where I clicked…as if the raycasthit is skipping over the enemy and ignoring the chance to attack…if I a am thinking of this correctly that might be what us causing the issue so that may have been changed by accident? I will look through and check just in case.

I hope this is on the right track. The logic and thought process sound right to me. I will see if I am missing anything and report back.

Does your enemy have a CombatTarget and a Capsule Collider?

Yes and Yes

Ah, I think I finally spotted it (! and no ! can be hard to spot, even after the 7th pass through the code…


        public bool CanAttack(CombatTarget combatTarget)
        {
            if (combatTarget == null)
            {
                return false;
            }
            Health targetToTest = combatTarget.GetComponent<Health>();
            return targetToTest != null && targetToTest.IsDead(); <------
        }

This will return true only if the target is dead. You want

return targetToTest!=null && !targetToTest.IsDead();
1 Like

It is working!!! Thank you so much!!

I wish I was able to spot these tiny details like this better. Practically any code error I run into, the problem always seems to be a simple code fix like this.

Thank you for your time in helping me find the error and with testing.

I will try to do better about not missing these details.

As I said, they can be hard to spot, especially the !.

Other things to watch out for are = vs ==, especially in if statements.

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

Privacy & Terms