- Home /
adding force every frame to control my ridigbody makes it shake ugly
hi guys, I am currently working on a gravity based racing game and got into some trouble with controlling my racer. I am trying to control it with the addForce function of the unity rigidbodys but adding force to the ridigbody every frame (tried both, update and fixed update functions) makes it shake. The rest of the game is still running fluid.
I need my racer to hover over the ground, so i cant attach invisible wheel collider and use some of the racing game tutorials
Does anyone know why the racer is shaking like this, or can give me a hint what different methods i can try to control the racer?
here's a video of the problem:
http://www.youtube.com/watch?v=AQgIjeKpbsU&feature=youtu.be
i hope someone can help a desperate German student with this.
the code:
void FixedUpdate(){
hud.setLives(actLives);
if(Time.timeScale != 0) {
Vector3 newForce = new Vector3(0,0,0);
if(gameStatehandler.gameState == GameStateHandler.GameStates.OnRace){
Vector3 velocity = transform.root.rigidbody.velocity;
// Basic controls
newForce+=new Vector3(driftForce*Input.GetAxis("Horizontal"),driftForce*Input.GetAxis("Vertical"),0);
//accelerate with Space of RT
if((Input.GetButton("Accelerate") || Input.GetAxis("AccelerateXbox") != 0))
{
if(velocity.z<actualMaxSpeed){
newForce+=new Vector3(0,0,acceleration*(actualMaxSpeed-velocity.z));
}
}
//if no acceleration, brake
else
{
if(transform.root.rigidbody.velocity.z > 0.0f){
newForce+=new Vector3(0,0,-transform.root.rigidbody.velocity.z*brakeFactor)*Time.deltaTime;
}
}
//if we have a maxSpeed boost and players velocity is lower than the maxSpeed limit, drop the speed limit
// to the current speed, but not below regular maxSpeed
if(transform.rigidbody.velocity.z > regularMaxSpeed)
actualMaxSpeed=Mathf.Clamp(actualMaxSpeed,regularMaxSpeed,velocity.z);
else
actualMaxSpeed = regularMaxSpeed;
//slowly reduse movement on X and Y axis
newForce += new Vector3(-velocity.x*0.1f,velocity.y*0.1f,0.0f);
//update HUD
hud.setVelocity(transform.rigidbody.velocity.z);
}
// if we are dead, set velocity to zero so the player does not do unwanted movements
else{
if(gameStatehandler.gameState == GameStateHandler.GameStates.Dead || gameStatehandler.gameState == GameStateHandler.GameStates.PreRace)
transform.rigidbody.velocity=new Vector3(0,0,0);
}
//hover it
RaycastHit hit;
LayerMask mask = 1<<LayerMask.NameToLayer("Respawn");
mask = ~mask;
if(Physics.Raycast(transform.position,Physics.gravity,out hit,3,mask))
{
newForce+=-Physics.gravity*Time.deltaTime*Mathf.Clamp(1.0f/(transform.position-hit.point).magnitude,1.0f,2.5f)*hoverfactor;
}
else{
//if there's no ground beneath us, add some additional force to the gravity
newForce+=Physics.gravity*6;
}
Debug.DrawLine( transform.position, transform.position+newForce);
transform.rigidbody.AddForce(newForce);
}
}
Answer by hoverTurtle · Aug 16, 2012 at 10:48 AM
nevermind, i found the solution yesterday :), the problem was, that i updated the camera position in the update function, but the forces added to the rigidbody in the fixedupdate function. I changed both to fixedupdate and now the movement is consistent and smooth.
Answer by VitorValadares · Aug 13, 2012 at 09:21 PM
I'm a newbie here, but I don't think you should be adding force all the frames. This way your space ship will be receiving force and loosing it (because of the natural gravity) all the times. Maybe this is the cause of the shaking effect.
Answer by hoverTurtle · Aug 13, 2012 at 09:58 PM
i did some research and found this codepart in the unity3d example project "3rdPersonshooter" from the unity3d website (a controller script for a flying orb enemy):
// Variables for positional and rotational stabilization
float prediction = 0.35f;
float force = 300.0f;
float torque = 100.0f;
// Move orb target position from old position to new
Vector3 position = oldFlyTarget * (1-flyLerp) + flyTarget * flyLerp;
// Add slight up and down motion
position += Mathf.Sin(Time.time * 2) * Vector3.up * 0.05f;
// Add force to control position
Vector3 forceVector = (position - (transform.position + rigidbody.velocity * prediction ));
rigidbody.AddForce(forceVector * force);
Debug.DrawLine(position, transform.position);
it works quite fine and is also in the fixed update function. He is calculating the new force and then adding it to the rigidbody However i cant find the relevant difference to my own code that lets the spaceship shake
Answer by dmitriyl · Apr 23, 2020 at 10:20 AM
I am porting my game from libgdx-box2d to unity and found equal wierd shaking, so I think it maybe an issue with box2d and how I use it. I also addForce every frame and update camera position every frame. Hovewer camera viewport doesn't shake relatively to other objects in scene, but rigidBody is shaked.
Your answer
Follow this Question
Related Questions
Artificial Gravity 2 Answers
Is time-based update necessary in FixedUpdate? 1 Answer
What's the Rigidbody's gravity unit? 1 Answer
Movement problem? 0 Answers