- Home /
Self Solved
Accelerating and Decelerating a Characters's speed
I'm looking to have a character accelerated when moving forward, decelerate when movement keys aren't pressed (keyboard/joystick), and slide when on certain surfaces.
I have managed to accelerate the player, but I'm having issues with the deceleration. I assume is because I'm multiplying the moveDirection Vector's forward and right (z and x) by Input.GetAxis (which becomes 0 when they are let go.)
After that I'll know how to code in the friction.
I've attached the segments of my player script that deals with movement exclusively. This is for a 3D platformer by the way, similar to Mario 64/Galaxy.
Any help would be wonderful. Thanks in advance!
//=====================================================================
// CalculateMovement
//=====================================================================
function CalculateMovement()
{
var jumpButton = Input.GetButton("Jump");
var jumpButtonPressed = Input.GetButtonDown("Jump");
var dodge = Input.GetKeyDown(KeyCode.DownArrow);
x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
y = moveDirection.y;
if (x != 0 || z != 0)
isMoving = true;
else if (x == 0 && z == 0)
isMoving = false;
horizontalSpeed = horizontalVelocity.magnitude;
verticalSpeed = controller.velocity.y;
overallSpeed = controller.velocity.magnitude;
var cameraTransform : Transform = Camera.main.transform;
var forward : Vector3 = cameraTransform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
var right = Vector3(forward.z, 0, -forward.x);
if ((!isAttacking) && canMove && isMoving)
{
moveDirection = x * right + z * forward;
}
if (moveDirection != Vector3.zero)
{
curSpeed += speed * Time.deltaTime;
curSpeed = Mathf.Clamp(curSpeed,0,maxSpeed);
}
if (!isMoving && canMove)
{
curSpeed -= (friction) * Time.deltaTime;
if (curSpeed < 0)
curSpeed = 0;
}
}
//=====================================================================
// ApplyMovement
//=====================================================================
function ApplyMovement()
{
// Move the controller
if (!busy))
{
var recalcMoveDir : Vector3 = Vector3(moveDirection.x,0,moveDirection.z)*curSpeed;
recalcMoveDir.y = moveDirection.y;
controller.Move((recalcMoveDir) * Time.deltaTime);
}
}
Answer by Bunny83 · Apr 01, 2012 at 08:17 PM
Well, the CharacterController has been invented so it's not affected by momentum or other external forces. If you want it to behave like a physic object you might want to use a Rigidbody instead. A Rigidbody can be moved via AddForce and it's automatically affected by friction and gravity. The friction can be modified by the PhysicMaterial of the surface.
Thanks. I'm aware that CharacterControllers aren't affected by physics forces naturally. I intended to code in formulas for when I wanted to create my own Physics (which I really only need for making the character accelerate, decelerate, and be affected by more, less or no friction). I'm using a character controller for the sake that it makes collisions in my game easier.
I've updated my code above so that it includes acceleration, but now I 'm having problems with the deceleration, which I assume is because I'm multiplying the moveDirection vector forward and right (z and x) by axis (which becomes 0 when they are let go.)
After that I'll know how to code in the friction.
Follow this Question
Related Questions
rigidbody2D with instant max speed and slow stop 1 Answer
How to decrease inertia for a rolling ball 1 Answer
What can I do to prevent momentum? 0 Answers
Character Movement 2 Answers
Spaceship movement with acceleration and deceleration on Unity 0 Answers