Page 1 of 1
How to Stop Player from Sliding Forward in Conversation
Posted: Mon Apr 11, 2022 3:46 pm
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
Re: How to Stop Player from Sliding Forward in Conversation
Posted: Mon Apr 11, 2022 5:05 pm
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 (36.26 KiB) Viewed 720 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.
Re: How to Stop Player from Sliding Forward in Conversation
Posted: Mon Apr 11, 2022 10:38 pm
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
Re: How to Stop Player from Sliding Forward in Conversation
Posted: Mon Apr 11, 2022 10:49 pm
by Tony Li
Glad to help!