The question is answered, right answer was accepted
Character Controller can accelerate but cant deccelerate
I've managed to get it so my character controller is able to accelerate when starting to move but it doesn't deccelerate at all when I let go of my xbox joystick. I thought it would work based off of what i've coded but it seems to be dependant on the input. If anybody can give me some advice as to how to get it to deccelerate I would be grateful. Thanks in advance :)
/////// Move
if (Player.isGrounded)
{
Vector3 Movement = new Vector3(HorizontalSpeed, 0, VerticalSpeed);
Movement = transform.rotation * Movement;
Player.Move(Movement * Time.deltaTime);
// Horizontal
// Right
if (Input.GetAxis("Left Joystick Horizontal") >= 1)
{
HorizontalSpeed += Time.deltaTime * Acceleration;
}
else
{
while (HorizontalSpeed > 0)
{
HorizontalSpeed -= Time.deltaTime * Acceleration;
}
}
// Left
if (Input.GetAxis("Left Joystick Horizontal") <= -1)
{
HorizontalSpeed -= Time.deltaTime * Acceleration;
}
else
{
while (HorizontalSpeed < 0)
{
HorizontalSpeed += Time.deltaTime * Acceleration;
}
}
HorizontalSpeed = Mathf.Clamp(HorizontalSpeed, -10, 10);
if (HorizontalSpeed > -0.4 && HorizontalSpeed < 0.4)
{
HorizontalSpeed = 0;
}
// Vertical
// Forward
if (Input.GetAxis("Left Joystick Vertical") >= 1)
{
VerticalSpeed += Time.deltaTime * Acceleration;
}
else
{
while (VerticalSpeed > 0)
{
VerticalSpeed -= Time.deltaTime * Acceleration;
}
}
// Back
if (Input.GetAxis("Left Joystick Vertical") <= -1)
{
VerticalSpeed -= Time.deltaTime * Acceleration;
}
else
{
while (VerticalSpeed < 0)
{
VerticalSpeed += Time.deltaTime * Acceleration;
}
}
if (VerticalSpeed > -0.4 && VerticalSpeed < 0.4)
{
VerticalSpeed = 0;
}
}
You might be confused about the behavior you'll get from this:
while (HorizontalSpeed > 0)
probably lol. I was trying to get it so when i have speed and then let go of the joystick it slowly moves it back to 0. But yeah I probably did it wrong. Would you know a way to get that functionality?
Answer by Landern · Nov 15, 2016 at 02:14 PM
Try removing the while loops which @TreyH was mentioning, if this is in the Update function it gets executed each frame, you're using deltaTime to smooth out the in/decrease of acceleration over time anyways, the while loops will pin the code in the loops until it is greater/less than whatever logic you put in there in a single frame or in the case of FixedUpdate step.
/////// Move
if (Player.isGrounded)
{
Vector3 Movement = new Vector3(HorizontalSpeed, 0, VerticalSpeed);
Movement = transform.rotation * Movement;
Player.Move(Movement * Time.deltaTime);
// Horizontal
// Right
if (Input.GetAxis("Left Joystick Horizontal") >= 1)
{
HorizontalSpeed += Time.deltaTime * Acceleration;
}
else if (HorizontalSpeed > 0)
{
HorizontalSpeed -= Time.deltaTime * Acceleration;
}
// Left
if (Input.GetAxis("Left Joystick Horizontal") <= -1)
{
HorizontalSpeed -= Time.deltaTime * Acceleration;
}
else if (HorizontalSpeed < 0)
{
HorizontalSpeed += Time.deltaTime * Acceleration;
}
HorizontalSpeed = Mathf.Clamp(HorizontalSpeed, -10, 10);
if (HorizontalSpeed > -0.4 && HorizontalSpeed < 0.4)
{
HorizontalSpeed = 0;
}
// Vertical
// Forward
if (Input.GetAxis("Left Joystick Vertical") >= 1)
{
VerticalSpeed += Time.deltaTime * Acceleration;
}
else if (VerticalSpeed > 0)
{
VerticalSpeed -= Time.deltaTime * Acceleration;
}
// Back
if (Input.GetAxis("Left Joystick Vertical") <= -1)
{
VerticalSpeed -= Time.deltaTime * Acceleration;
}
else if (VerticalSpeed < 0)
{
VerticalSpeed += Time.deltaTime * Acceleration;
}
if (VerticalSpeed > -0.4 && VerticalSpeed < 0.4)
{
VerticalSpeed = 0;
}
}
Follow this Question
Related Questions
My Character won't jump 0 Answers
Raycast fall detection and issues with .isGrounded, 1 Answer
Help on how to code crouching? 0 Answers
2D plaformer, Can bug me into wall 2 Answers