Dialogue Only When Player Is Grounded

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
PayasoPrince
Posts: 104
Joined: Thu Jan 27, 2022 6:47 pm

Dialogue Only When Player Is Grounded

Post 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? :?:
User avatar
Tony Li
Posts: 21981
Joined: Thu Jul 18, 2013 1:27 pm

Re: Dialogue Only When Player Is Grounded

Post 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();
    }
}
User avatar
PayasoPrince
Posts: 104
Joined: Thu Jan 27, 2022 6:47 pm

Re: Dialogue Only When Player Is Grounded

Post 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? :?:
User avatar
Tony Li
Posts: 21981
Joined: Thu Jul 18, 2013 1:27 pm

Re: Dialogue Only When Player Is Grounded

Post by Tony Li »

I don't foresee any issues. That should be totally future proof.
Post Reply