Forum rules - please read before posting.

Character SetLookDirection - Instant?

Hi all

Unity 2D game. Working on an NPC.

I've got a question regarding just how instant the SetLookDirection function is in Char. Directly after this I am running a custom action that relies on the character being in a particular direction. That direction is Left. The NPC's default position is Up, as all characters are.

In the code first I run the SetLookDirection function, which should change the character's direction to left (Vector3(-1, 0, 0)). The line underneath this is the GetSpriteDirectionInt function, which should now be 1 (Left). But instead I am getting 3 (Up).

Does the SetLookDirection function not change the direction immediately, but just change some variables that get read on the next _LateUpdate call? Or in other words, does the character's direction only actually change on the next frame in _LateUpdate where the code that gets run in that single action (following the SetLookDirection function call) is called first?

Hope that makes sense. It's a bit confusing.

Comments

  • Are you adding an additional "true" as the second parameter?  That tells AC to make the change instantly.
  • Yes I am.
  • Though the change should be near-instantaneous, the handling of the character's direction and which animation to play is handled through different threads - so it may indeed require an additional Update/_LateUpdate call.

    However, since both of these functions are public on the Char script, you should be able to simply call these after the SetLookDirection to force the change that frame.
  • Firstly, apologies for the terrible formatting of code in this post. I don't know how to make code look good on these forums.

    Ok I think I may have gotten onto something, and it doesn't appear to have anything to do with how "instant" things are, but rather about the charState being Custom... but let me know if I'm on the wrong track altogether...

    I am running a custom animation (so charState is CharState.Custom) and flipCustomAnims is set to false.

    In Char.cs, inside the PrepareSpriteChild method:

    if (charState == CharState.Custom && !flipCustomAnims)
    {
    flipFrames = false;
    }
    else
    {
    if (!lockDirection)
    {
    spriteDirection = SetSpriteDirection (rightAmount, forwardAmount);
    }
    ...

    Basically, given those conditions I mentioned above:
    if (!lockDirection)
    {
    spriteDirection = SetSpriteDirection (rightAmount, forwardAmount);
    }
    is never run. Perhaps this code needs to be put inside the code above:
    if (charState == CharState.Custom && !flipCustomAnims)
    {
    flipFrames = false;
    }

    I put that in there and everything worked perfectly.

    Have I created more problems by doing this or does that make sense?
  • edited April 2016
    This is the first you've mentioned of your character's charState being Custom (you only said the Action was custom).  That should indeed prevent the sprite direction from being changed, since its now under manual control.

    When Custom, a character has no concept of direction, it'll just play whatever animation you tell it.  If you want to change a character's direction, make sure their charState is set back to Idle.
  • Yes I didn't realise the charstate was custom until I did some digging. My custom action is mostly a duplicate of Character: Animate, and I am using the PlayCustom method.
    The reason direction matters for me is because my 2d character's Sprite child gameobject changes according to the direction the character is facing. I am using spine animations and my specific animations sit within each direction data, so it's dependent on the direction being set. A bit hard to explain technically without examples, but it doesn't really matter - the point is I am using character directions not just for idle or walk facing, but also other custom animations.
    I will change the direction right before the charstate changes to Custom, but moving on is there a way we could change it so that directions can be changed even when the charstate is Custom and not Idle?
  • The SetSpriteDirection function referenced in the code you public is public (see its entry in the scripting guide) - have you tried calling that instead?
  • I just moved the 'charState = CharState.Custom' to after I change the direction while the charState is still Idle. It works well.

    But yes I can see that if I ever need to force the direction change whilst in CharState.Custom that is a good solution, as long as I can somehow access the rightAmount and forwardAmount variables.
  • BTW, thanks for your help Chris. :)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.