#or i should just fix my logic and keep

1 messages · Page 1 of 1 (latest)

fierce rose
#

I would not recommend it
But either in code or in the animator you will have to have a system that determines which animation to play when there are overlapping parameters
And more importantly you have to decide that in design
What should your character do at that moment

#

Animator as a finite state machine has tools to determine those cases
With code you'd be creating the same systems from scratch

potent salmon
#

i only know that
if X distance is Low a sight animation must play
if X value is set a chase animation must play
if shoots a shooting animation must play
if is in the function hurt a hurt animation must play

fierce rose
#

That is an enemy AI, I assume

potent salmon
#

yep

#

i mean, everything uses animator.play, but i want to focus on the enemy first for be "organized step by step"

fierce rose
#

Generally you have a FSM or a behaviour tree for AI that in some way prevents overlapping of actions

#

(Or any system though FSMs and behaviour trees are a good fit)

potent salmon
#

behaviour tree?

#

is like statemachine?

fierce rose
#

Similar

potent salmon
#

idk, i just did a lot of if scenarios and functions

potent salmon
fierce rose
#

It's a programming pattern
If you're not familiar with those in general, FSMs may be a simpler option

potent salmon
#

FSM?

#

oh, statemachine

potent salmon
fierce rose
#

They're not necessary, but when your AI can have discreet states of behavior that should have contextual priority, it's going to be a mess to code with just if statements and timers

#

If the AI states can overlap, having a robust animator state machine that doesn't allow overlaps won't solve the underlying problem

#

If the enemy swaps rapidly between moving and shooting for example, the animator can only do so much to smooth it out

#

AI characters often have more animation states than AI behavior states, which is why it helps to have the systems be separate

#

But if the animations and AI states map one to one to each other, then there's not really a benefit to doing anything more fancy than calling the specific animations to play directly

potent salmon
#

oh

#

then the problem is my script

#

not my animations

fierce rose
#

Probably

potent salmon
#

huh, thats easier, i hope is that, thx

fierce rose
#

It's hard to read when it's not in english and not following C# formatting conventions very well

#

And not commented either

potent salmon
#

c# formatting?

fierce rose
potent salmon
#

i thought the format was

start with lowercase, next word with uppercase

potent salmon
#

i literally one by one changed it from capsle to lowercase ;-;

#

ou

fierce rose
#

Most IDEs let you rename variables and methods across the whole class
Or even across the project

#

And let you type/edit in multiple places with multiple carets and whatnot

#

It's recommended that booleans start with a verb but that's not that big of a deal as long as it's clear

potent salmon
#

makes sense

#

well so, this means my script theory is just...bad

fierce rose
#

Possibly

#

You didn't actually mention what the problem is

potent salmon
#

my animations overaps

fierce rose
#

But how

#

What animation

#

The AI is so simple it might not need a whole state machine but you can probably see how easily you will get tangled up if you need a few more variables to track and to choose between with more if statements

potent salmon
#

let me see if i can remember, when i add that the patrol state plays an animation of...patroling, it makes the sigh to never fully trigger to wich makes it to stay frozen

#

when i add a hurt animation, it just gets, frozen, in it hurt animation

#

is odd

fierce rose
#

Animator could transition from hurt state to whichever state needed automatically
With code you'd have to track that and call that manually

potent salmon
#

oh

#

makes sense i guess

fierce rose
#

Which is why I think Animator is a better tool for the task

potent salmon
#

huh

fierce rose
#

It's easy to get tangled up with that too though, and most tutorials for it suck

potent salmon
#

but now i got slighty lost

i have to fix my enemy logic, or i have to make it use the Animator States instead of just play

fierce rose
#

Probably both

potent salmon
#

ou

fierce rose
#

Try to find out what's wrong with your enemy logic first, if anything

#

It seems you don't really know what's causing the issue you're seeing

potent salmon
#

exactly, i mean, i feel like i know it

fierce rose
#

I have just a part of the AI and a vague description of the issue so I can't do much

potent salmon
#

but that with the escase knownledge i have, it will be a ride to take it out

potent salmon
#

thats, o h

#

right, the base

#

holup

#

from here the enemies pick up functions and stuff

#

it was my way for make designing them more easier and fun

fierce rose
#

I don't have the variables either
And I can't see how the player is interacting with it
And I can't see what the animations actually do

#

And I haven't seen what the issue looks like

potent salmon
#

the player only triggers the takedamage function, no big deal
the animations just...are visual, except sight who ends has an animation event to the chase function

#

and i will now make a video about the issue

#

actually for now is just the enemy hurt animation who has an issue

fierce rose
#

I didn't see a call to Play any "hurt" animation

potent salmon
potent salmon
# fierce rose I didn't see a call to Play any "hurt" animation

is justt instead of

    {
        enemyCry(trooperhurt1, trooperhurt2, trooperhurt3, audioSource, 1f, 1.5f);
        health -= damage;```

is

```public override void TakeDamage(float damage, int type, Transform attackposition)
{
    animator.Play("mortemRifleHurt");
    enemyCry(trooperhurt1, trooperhurt2, trooperhurt3, audioSource, 1f, 1.5f);
    health -= damage;```
#

also when in the patrol function i put it to play the patrol animation

wich was my main concern, the enemy never triggered the sigh canimation function cuz it was overlapping one to another

this is the modified script that doesnt allow to enter in the sight animation event

   flipBySight();
   animator.Play("Base Layer.MortemRiflePatrol");
   enemyPatrol(mortemRb, mortemtrooperpatrolspeed, mortemtrooperTimer);
   if (distanciaAbsoluta < rangoVision && detectoValeria == false)
   {
       animator.Play("Base Layer.MortemRifleSight");
       audioSource.PlayOneShot(troopersight);
       detectoValeria = true;
   }```
#

and i need to understand better whe way my animations work, cuz im planning on echance their movement

#

btw everything's a placeholder

fierce rose
#

Since the animations are short and the movements are rapid, it really looks likely that the enemy could go between shooting, chasing, patrolling and being hurt from frame to frame or even try to do all at once

#

Maybe what you'd want is for the AI to stay in the shooting state for a certain amount of time before being able to transition to another state

#

Unless interrupted by being hurt, for example, which also pauses the behavior

#

That's why finite state machines are handy for this

#
    void mortemtrooperChase()
    {
        Debug.Log("it should be chasing you");
        chasing = true;
        if (detectedValeria == true)
        {
            if (absoluteDistance > shootingRange)
            {
                animator.Play("MortemRifleShooting");
                chaseAction(mortemRb, mortemtrooperRunSpeed);
                dispararMortem();
            }
            else
            {
                animator.Play("MortemRifleShooting");
                stopChasing(mortemRb);
                dispararMortem();
            }
        }
    }```
#

Both sides of the if statement play shooting animation?

#

Also

        chasing = true;
        if (detectedValeria == true)

in a method that's called when

        if (detectedValeria == true && chasing == true)
        {
            mortemtrooperChase();
        }
#

both of those bools are always true to begin with

#

If the method would be called

potent salmon
#

yeah, i didnt thought that thru...

fierce rose
#

So yes the script could be better

#

Learning to code an FSM may help here, but not totally necessary if the AI will be this simple in the future as well
You can do state-like behavior with timers

#

But for anything more complex than this probably not worth taking that shortcut

#

Because your animations would not need blending or to deal with overlapping, using Play() is probably sufficient over a proper animator controller with parameters and transition conditions

#

But skipping the animator is also similarly a shortcut that will become a burden with any more complexity

#

Animator takes some learning but I'd always use it at least somewhat

fierce rose
potent salmon
#

i meant for the timer thing

potent salmon
fierce rose
potent salmon
#

oh, the delta thing

#

ok

#

but that was for the firing speed, but ok

potent salmon
#

correct?

fierce rose
#

But if actions (or "states") can't overlap, then the same timer works for whichever is the current state

#

Each state could set the timer to a specific value which when counting down checks for condition again
Unless a higher priority state overrides it

potent salmon
#

oooh, i think i get it, is the wait

#

but how would an example look like?

#

cuz despite i understood the logic i felt slighty confused abt the code

fierce rose
#

Either it's a web of if elses or an FSM implementation

potent salmon
#

isnt it the same just using FSM format?

#

btw how i convert a if web into a FSM?

fierce rose
fierce rose
#

You can find theory about FSMs both in general programming context and in C# context
And also practical examples for implementing them in Unity monobehaviours specifically

#

FSM pattern does not require making an FSM tool, which you may also find guides or resources for

potent salmon
fierce rose