- Home /
Setting rigidbody.velocity giving unexpected result.
I am trying to implement a 2.5D platformer script:
I am clamping the rigidbody's velocity.x between a certain value so that it just doesn't accelerate for ever . In this case it is from 0.0f to 0.8f units.
adding a force to move the player.
All this are being done in FixedUpdate, which is set to a FixedDelta of .01f in projects settings.
Jumping is done by setting velocity.y directly.
Drag is set to 0.0f
The problem is, clamping the velocity causes the character to lose it's momentum. So to fix it I am storing the velocity.x component from the time of jump and setting it directly while player is in air. Now, it works all fine except that I need to scale the velocity.x component by some value ( 8.0 in my particular case) to make the player have momentum in air. If I set it to my maximum move speed ( 0.8f in my case) it almost doesn't move while in air. But when grounded, it moves normally by setting that value directly.
Now the problem isn't that the velocity.x value isn't assigned correctly. Because I have checked many times (by Debug.Log as well as printing to guiText) that the velocity.x of the character, while it is in air, is exactly what it should be. But for some reason it is not moving in that speed along the X axis.
Relevant code: [ temVelocity is restored and assign`enter code here`ed in the beginning and end of FixedUpdate respectavily ]
void FixedUpdate()
{
temVelocity = rigidbody.velocity; // get ref for future use
/////////////////// JUMP TIMER ///////////////////////
//update timer in fixed update to make jump deterministic
if (onAir)
jumpTimer.Update(Time.fixedDeltaTime);
//check if jump time remaining
if (jumpTimer.isOver())
{
canJump = false;
}
////////////////// END ///////////////////////
/////////////// MOVEMENT TIMER ////////////////////
//movement stopped so reset movement timer
if (Input.GetAxisRaw("Horizontal") == 0)
{
movementTimer.reset();
movementTimer.Target = int.MaxValue;
}
//player moving , so update movement timer
if(Input.GetAxisRaw("Horizontal")!=0)
{
movementTimer.Update(Time.fixedDeltaTime);
}
////////////////////// END //////////////////////////
//when on ground and moving
if (!onAir && Input.GetAxisRaw("Horizontal") != 0)
{
dir = (int)Input.GetAxisRaw("Horizontal"); // get face direction
rigidbody.AddForce(Input.GetAxis("Horizontal") * transform.right * moveForce, ForceMode.Force);
//movement timer functions
movementTimer.Update(Time.fixedDeltaTime);
}
if(onAir)
{
//released movement while in air; should start to loose momentum
if(Input.GetAxisRaw("Horizontal")==0)
{
loseMomentum = true;
}
//momentum losing process
if(loseMomentum)
{
releaseSpeedX -= dampingFactor;
releaseSpeedX = Mathf.Clamp(releaseSpeedX, 0, int.MaxValue);
}
//set momentum of player
temVelocity.x = releaseSpeedX*8f * dir; //dir has been stored while on ground and never changed in air
}
//clamp speed whlie grounded
if(!onAir && Mathf.Abs(rigidbody.velocity.x)>speedLimit )
{
temVelocity.x = speedLimit * Mathf.Sign(temVelocity.x) ;
}
//when player can jump and is trying to jump
if (canJump && Input.GetAxisRaw("Vertical") == 1)
{
temVelocity.y = jumpSpeed;
//store value to use as momentum at the moment of jump; interpolate between min and max possible value
if (!jumped)
{
if (Input.GetAxisRaw("Horizontal") != 0)
{
float temDelay = Mathf.Clamp(movementTimer.Time, 0, 1f);
releaseSpeedX = Mathf.Lerp(speedLimit / 2f, speedLimit, temDelay);
loseMomentum = false; //retaining momentum
jumped = true;
}
else // no direction chosen, jump upwards
{
releaseSpeedX = 0f;
jumped = true;
loseMomentum = false;
}
}
}
//text.GetComponent<GUIText>().text = rigidbody.velocity.x +" "+ releaseSpeedX;
//player on ground and is NOT moving, stopping flag allows not to be constantly applying stopping velocity
if(stopping && !onAir && Input.GetAxisRaw("Horizontal")==0)
{
stopping = false;
text.GetComponent<GUIText>().text = "yes";
temVelocity.x = 0;// * Mathf.Sign(tem.x);
}
else if(Input.GetAxisRaw("Horizontal") !=0)
{
stopping = true;
}
rigidbody.velocity = temVelocity; //reassign velocity
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613184234im_/https://answers.unity.com/themes/thub/images/avi.jpg)