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
0
Question by sacapuces · Aug 10, 2012 at 12:22 AM · movementphysicsrigidbodyforce

Erratics movements from my rigidbody : why does it behave like that?

Hello. I'm making a basic MoonLander in 2D space. For that I control a spaceship by applying force or torque to its rigidbody. For some reason, when my object is spinning and moving, after a while it become crazy, and near teleport in random position. Here's a video to show you what it looks like :

http://www.youtube.com/watch?v=E-f7RfywI-k&feature=youtu.be

and my code : i din't include CollisionSettings because it's only used for collisions but the rest is as it is

 class ThrustSettings{
  var positiveThrust : int = 300;//define, in percentage, the power of the thrust over the gravity : acceleration = thrust/gravity
  var negativeThrust : int = 100;
  var sideThrust : int = 100;
  
  function ComputeThrust(input : float) : float{
  if(input>0.01) return positiveThrust;
  else return negativeThrust;
  }
 }
 
 class RotationalSettings{
  var angularAcceleration : float = 10;//the angular acceleration, in degree/sec^2
  var normalDrag : float = 0.0;
  var stabilizerStandardDrag : float = 1.0;
  var stabilizerStopDrag : float = 10.0;
  var maxStabilizerSpeed : float = 5.0;
  var stoppingSpeedStabilizer : float = 0.3;
  var stabilizerOn : boolean = true;
  
  //return the correct rotationnal drag
  function ComputeDrag (velocity : Vector3, input : float) : float{
  //check that the stabilizer is on, and that there is no input, and that the rotation speed isn't too high
  if(stabilizerOn && Mathf.Abs(input)<0.01 && Mathf.Abs(velocity.z*360/6.28)<maxStabilizerSpeed){
  if(Mathf.Abs(velocity.z*360/6.28)<stoppingSpeedStabilizer) return stabilizerStopDrag;
  return stabilizerStandardDrag;
  }
  else return normalDrag;
  }
 }
 
 /*End of classes definitions
 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 */
 
 
 var positionalMovement : ThrustSettings;
 var rotationalMovement : RotationalSettings;
 var collisionSettings : CollisionSettings;
 var canControl : boolean =true;
 var tankSize : int = 30;//each unit here represent one second of full main thrust
 @HideInInspector
 var fuelLeft : float;
 var hullPoints : int = 100;
 var spawnPoint : GameObject;
 
 function Start(){
  collisionSettings.Setup(this.gameObject);
  fuelLeft=tankSize;
 }
 
 function FixedUpdate () {
  //retrieve input (range -1..1) and scale them to match gravity or degree instead of radian
  verticalThrust = Input.GetAxis("Vertical")*Physics.gravity.magnitude;
  lateralThrust = Input.GetAxis("Horizontal")*Physics.gravity.magnitude;
  turn = Input.GetAxis("Rotational")*2*3.14/360;
 
  if(fuelLeft<=0 && canControl) canControl = false;
  if(!canControl){verticalThrust=0.0; lateralThrust=0.0; turn = 0.0;}
  fuelLeft=Mathf.Max(0, fuelLeft-(verticalThrust+lateralThrust+turn)*Time.deltaTime);
  
  // compute the correct thrust force, depending on it's direction (up or down), then add correct relative vertical force to the rigidBody
  //also add lateral thrust if any
  rigidbody.AddRelativeForce(lateralThrust * positionalMovement.sideThrust/100,
  verticalThrust * positionalMovement.ComputeThrust(verticalThrust)/100,
  0, ForceMode.Acceleration);
  //add torque
  rigidbody.AddRelativeTorque(0, 0, turn * rotationalMovement.angularAcceleration, ForceMode.Acceleration);
  //Compute the correct drag for rotation
  rigidbody.angularDrag=rotationalMovement.ComputeDrag(rigidbody.angularVelocity, turn);
 }
 
 function SetSpawnPoint(spawn : GameObject){
  this.spawnPoint = spawn;
 }
 
 function ReceiveDamage(value : int){
  hullPoints=Mathf.Max(hullPoints-value, 0);
  if(hullPoints==0){
  Crash();
  }
 }
 
 function Crash(){
  if(spawnPoint){
  Destroy(this.gameObject);
  spawnPoint.SendMessage("Respawn", spawnPoint);
  }
  else Debug.LogError("no spawn point given to respawn", this.gameObject);
 }
 
 function OnCollisionEnter(collision : Collision) {
  for(contact in collision.contacts){
  collisionSettings.ApplyDamage(collision.relativeVelocity, contact.thisCollider);
  collisionSettings.UpdateTempColliders(contact);
  }
  collisionSettings.Switch();//this switch tempColliders with usedColliders and reset tempColliders
  //print("Collision enter --> " + collisionSettings.GetUsedColliders().ToString());
 }
 
 function OnCollisionStay(collision : Collision) {
  for(contact in collision.contacts){
  Debug.DrawRay(contact.point, collision.relativeVelocity);
  collisionSettings.UpdateTempColliders(contact);
  collisionSettings.Compare(contact, collision.relativeVelocity);
  }
  collisionSettings.Switch();
 }
 
 function OnCollisionExit(collision : Collision){
  collisionSettings.ClearColliders();
 }
 
 
 @script RequireComponent (Rigidbody)
Comment
Add comment · Show 1
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 adriandevera · Jun 28, 2015 at 07:36 AM 0
Share

Anyone have a solution to this?

0 Replies

· Add your reply
  • Sort: 

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

8 People are following this question.

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

Related Questions

Kinematics: Trying to get an object to land at a known point 1 Answer

Predicting land position with drag applied 0 Answers

What is the best way to apply force, then come to a smooth stop on control release? 1 Answer

Calculating required force for pushing a body to a desired position at once 0 Answers

Player is Speeding up in collisions 0 Answers


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