- Home /
'jerking' movement...
Ok, i have a very simple movement control on my character (with rigidbody):
var moveSpeed : float = 10;
function Update () {
if(Input.GetButton("forward")){
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
}
Couldn't be simpler, right?
But when i press 'forward', the model 'jerks' forward a little...how do we stop this?
I've tried using -
'Time.smoothDeltaTime'
a simple slow acceleration method (increasing moveSpeed slowly over time until we reach top speed),
but there is still that small 'jerk' forwards.
A system with 'AddRelativeForce' solves the problem, but raises many others, so if possible i'd like to solve the problem without going to AddForce.
Thank you for your time, Tom :)
Answer by robertbu · Feb 28, 2013 at 08:30 AM
Here are a couple of scripts...nearly identical. Is this the kind of movement you are looking for? Does this solve your jerkiness.
Start with a new scene. Place a cube at (-5,0,0) and rotate it 90 degrees on the Y axis. Use the Left Arrow.
var moveSpeed : float = 0.0;
var incSpeed : float = 0.02;
var maxSpeed : float = 2.0;
function Update () {
if(Input.GetKey(KeyCode.RightArrow)){
moveSpeed += incSpeed;
}
else {
moveSpeed -= incSpeed;
}
moveSpeed = Mathf.Clamp(moveSpeed, 0.0, maxSpeed);
transform.position += transform.forward * moveSpeed * Mathf.Sin(moveSpeed/maxSpeed * Mathf.PI / 2.0) * Time.deltaTime;
}
And:
var moveSpeed : float = 0.0;
var incSpeed : float = 0.01;
var maxSpeed : float = 2.0;
function Update () {
if(Input.GetKey(KeyCode.RightArrow)){
moveSpeed += incSpeed;
}
else {
moveSpeed -= incSpeed;
}
moveSpeed = Mathf.Clamp(moveSpeed, 0.0, maxSpeed);
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
That first one was a huge help :)
I've never used $$anonymous$$athf.Sin or .PI, i best pay em more attention :)
There's many easing functions. The Sine function tends to "look right" more often, so I tend to use it first, but you might want to try some of the others. Here is a link to source for many different easing functions: