I’m using this code to make my character dash
if(Input.GetKeyDown(KeyCode.LeftControl)){
rb.AddForce(moveInput.normalized * 60000);
Debug.Log("dodged");
}
It works but looks more like a teleport. How would I go about smoothing this down so it looks more like a smooth sliding motion?
If you’re looking for a consistent dodge that happens over time, there’s a couple ways to do it. You could use a coroutine or you could use Update and a “dodge flag”. For example, a coroutine could look like this:
/// <summary>
/// Dodge distance/speed that gets processed every frame. Might wanna keep this low!
/// </summary>
private float _dodgeSpeed = 60000;
/// <summary>
/// How long dodges last.
/// </summary>
private float _dodgeTime = 0.5f;
/// <summary>
/// Reference to the dodging coroutine.
/// </summary>
private Coroutine _dodging;
private void Update()
{
if (Input.GetKeyDown(KeyCode.LeftControl) && _dodging == null)
{
_dodging = StartCoroutine(DodgeCoroutine());
}
}
private IEnumerator DodgeCoroutine()
{
var endOfFrame = new WaitForEndOfFrame();
var rigidbody = GetComponent<Rigidbody>();
for (float timer = 0; timer < _dodgeTime; timer += Time.deltaTime)
{
rigidbody.MovePosition(transform.position + (transform.forward * (_dodgeSpeed * Time.deltaTime)));
yield return endOfFrame;
}
_dodging = null;
}
For an Update() implementation you would do the same thing but replace the _dodging Coroutine with a _dodging boolean. Then, if (_dodging) aforementionedLogic();