Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by hoverTurtle · Aug 13, 2012 at 08:11 PM · rigidbodygravityforcefixedupdate

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);
  }
  }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sajalsh25 · Jul 10, 2016 at 05:07 AM 0
Share

THAN$$anonymous$$YOU SOO $$anonymous$$UCH <3

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Realistic/No gravity spaceship movement? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges