- Home /
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)
Your answer
Follow this Question
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