How to Stop Player from Sliding Forward in Conversation

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
DrewThomasArt
Posts: 60
Joined: Thu Mar 24, 2022 12:07 am

How to Stop Player from Sliding Forward in Conversation

Post by DrewThomasArt »

Right now, I'm freezing the player by making his Rigidbody go to sleep at the start and wake up at the end of a conversation with Dialogue System events.

The problem is, when he starts a conversation in the middle of a jump he is frozen in the air and it looks weird (because his animation state goes to idle also). But if I don't freeze his Rigidbody, he slides to Narnia.

Is there a way to keep his gravity falling to the ground but still keep him from moving/sliding during dialogue?

Thank you
User avatar
Tony Li
Posts: 21980
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to Stop Player from Sliding Forward in Conversation

Post by Tony Li »

Hi,

You can add a script like this to the player:

StopCharacterVelocity.cs

Code: Select all

using UnityEngine;

public class StopCharacterVelocity : MonoBehaviour
{
    public void Stop()
    {
        var rb = GetComponent<Rigidbody>();
        if (rb != null)
        {
            rb.velocity = rb.angularVelocity = Vector3.zero;
        }
    }
}
Then configure Dialogue System Events to call StopCharacterVelocity.Stop:

stopCharacterVelocity.png
stopCharacterVelocity.png (36.26 KiB) Viewed 686 times

You can also modify the script to do the same thing without having to set up Dialogue System Events:

StopCharacterVelocity.cs

Code: Select all

using UnityEngine;

public class StopCharacterVelocity : MonoBehaviour
{
    public void OnConversationStart(Transform actor)
    {
        Stop();
    }
    
    public void Stop()
    {
        var rb = GetComponent<Rigidbody>();
        if (rb != null)
        {
            rb.velocity = rb.angularVelocity = Vector3.zero;
        }
    }
}

If you're making a 2D game, use Rigidbody2D instead of Rigidbody, and use Vector2.zero instead of Vector3.zero.
DrewThomasArt
Posts: 60
Joined: Thu Mar 24, 2022 12:07 am

Re: How to Stop Player from Sliding Forward in Conversation

Post by DrewThomasArt »

Tony Li wrote: Mon Apr 11, 2022 5:05 pm Hi,

You can add a script like this to the player:

StopCharacterVelocity.cs

Code: Select all

using UnityEngine;

public class StopCharacterVelocity : MonoBehaviour
{
    public void Stop()
    {
        var rb = GetComponent<Rigidbody>();
        if (rb != null)
        {
            rb.velocity = rb.angularVelocity = Vector3.zero;
        }
    }
}
Then configure Dialogue System Events to call StopCharacterVelocity.Stop:


stopCharacterVelocity.png


You can also modify the script to do the same thing without having to set up Dialogue System Events:

StopCharacterVelocity.cs

Code: Select all

using UnityEngine;

public class StopCharacterVelocity : MonoBehaviour
{
    public void OnConversationStart(Transform actor)
    {
        Stop();
    }
    
    public void Stop()
    {
        var rb = GetComponent<Rigidbody>();
        if (rb != null)
        {
            rb.velocity = rb.angularVelocity = Vector3.zero;
        }
    }
}

If you're making a 2D game, use Rigidbody2D instead of Rigidbody, and use Vector2.zero instead of Vector3.zero.
Thank you very much :) Greatest customer support on Earth
User avatar
Tony Li
Posts: 21980
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to Stop Player from Sliding Forward in Conversation

Post by Tony Li »

Glad to help! :-)
Post Reply