- Home /
Why does my character only dash forward and not in the moving direction?
Its probably a simple fix but i cant see it, can someone help me by just fixing the dash, when running/walking and pressing the dash button, player only dashes forward disregarding the facing or move direction? Thank you good people!
private void Dash()
{
currentDashTime = 0.0f;
if(currentDashTime < maxDashTime)
{
moveDirection = transform.forward * dashForce;
currentDashTime += dashStoppingSpeed;
}
else
{
moveDirection = Vector3.zero;
}
controller.Move(moveDirection * Time.deltaTime*dashForce);
}
Is this script on your controller? Just make sure this script would be on the object that is rotating. If its not you would need to adjust what object you are using the forward vector of.
Answer by TreyH · Feb 18, 2019 at 03:32 PM
Yeah, transform.forward
will give you the forward direction of whatever scene object contains that script. If you're doing something like keeping a single parent object with movement / game scripts and then a child object with all of its scene representation, then you'll need to specify that you want to use that object's direction.
What I think you want is:
private void Dash()
{
currentDashTime = 0.0f;
if(currentDashTime < maxDashTime)
{
// You don't necessarily want "transform.forward", but the "controller.transform.forward",
// since this is the object actually moving.
//
// Also, you were multiplying by "dashForce" here and in the controller.Move line,
// which is probably an accident. With a variable name like "moveDirection",
// it's more readable / intuitive that this just be a unit vector providing orientation
//
moveDirection = controller.transform.forward;
currentDashTime += dashStoppingSpeed;
}
else
{
moveDirection = Vector3.zero;
}
controller.Move(moveDirection * Time.deltaTime*dashForce);
}