Page 1 of 1

Dialogue Only When Player Is Grounded

Posted: Fri Jan 28, 2022 2:08 pm
by PayasoPrince
Hello,

I do not want my player to have the option of starting dialogue if they are jumping.

Is there a way I can check for "if(playerIsGrounded)" before giving the dialogue option? :?:

Re: Dialogue Only When Player Is Grounded

Posted: Fri Jan 28, 2022 2:36 pm
by Tony Li
Hi,

Here are two different ideas for doing that:

1. Disable the ProximitySelector component while jumping. (This is probably the simplest solution.)

2. Or make a subclass of ProximitySelector and override UseCurrentSelection(). Example:

Code: Select all

public override void UseCurrentSelection()
{
    if (playController.playerIsGrounded) 
    {
        base.UseCurrentSelection();
    }
}

Re: Dialogue Only When Player Is Grounded

Posted: Sat Jan 29, 2022 7:19 pm
by PayasoPrince
Thanks for the follow up, Tony!

I ended up going the route of making a subclass for the ProximitySelector.

This way, I was able to integrate the dialogue system into my custom PlayerController Class. My player states are driven by a state machine. I created a new speaking state that disables movement and sets the animator to play the Idle animation.

So, to hook this all up, I did this:

Code: Select all

if (playerController.myController.isGrounded)
            {
                playerController.stateMachine.ChangeState<HDialogueState>();
                base.UseCurrentSelection();
            }
then, I added an OnConversationEnd() event to leave the speaking state and gain control by returning to the idle state.

This worked, however, it created a problem. I didn't realize that "UseCurrentSelection()" would always execute. Even if you weren't in range of a usable NPC. So when I pressed the firekey at a random point in the map, my player would always enter the speaking state and get stuck with no control.

To fix this, I added the condition

Code: Select all

"if ((currentUsable != null) && currentUsable.enabled && (currentUsable.gameObject != null) && (Time.time >= timeToEnableUseButton))"
All together, it looks like this:

Code: Select all

public override void UseCurrentSelection()
    {
        if ((currentUsable != null) && currentUsable.enabled && (currentUsable.gameObject != null) && (Time.time >= timeToEnableUseButton))
        {
            if (playerController.myController.isGrounded)
            {
                playerController.stateMachine.ChangeState<HDialogueState>();
                base.UseCurrentSelection();
            }
        }
    }
Seems to be working perfectly now. However, I would like to make sure its future proof.
Are there any issues with how I am currently doing this? :?:

Re: Dialogue Only When Player Is Grounded

Posted: Sat Jan 29, 2022 8:48 pm
by Tony Li
I don't foresee any issues. That should be totally future proof.