Search Unity

Spaceship flight works perfectly until certain speed is achieved...then BOOM :(

Discussion in 'Physics' started by Palimon, Mar 13, 2015.

  1. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    Hi, my goal is to have a spaceship flyable via rigidbody physics, and rigidbody stuff inside: players, boxes, etc move with the spaceship as it accellerates, yet be able to move around on their own. I am SO close to achieving this! In fact, it is almost working perfectly. I'm hitting a strange bug though. I attached an image of the relevant portion of my Unity hierarchy. I have a spaceship with a few items as children (some rigidbodies, some not). Mesh, Player and RB are rigidbodies. I have the below script attached to Mesh. The topOfShip variable is set to Spaceship 1.

    The behavior I'm seeing is that I can fly the ship correctly, and everything follows perfectly, with force scaled appropriately for all RigidBody elements. However, when I get moving at a certain rate (aka, after I apply enough force) the Player and RB box go shooting off like bats out of Hell! I have no idea why. (I'll estimate this threshold rate at about 15mph.) Even stranger, when I have a mouselook script on my Player this happens at a much lower force threshold (I'll say 5mph); prior to this lower threshold the application of force to objects via the ship flying AND controlling the player both work perfectly. To be clear, I do still run into this problem when the player controller script is completely removed. I've spent about 30hrs looking at this and I'm going crazy! Please help! If any other information, or even a copy of my project, would help, let me know and I'll post or send it to you.

    Code (CSharp):
    1. // Update is called once per frame
    2.     void FixedUpdate()
    3.     {
    4.         if (on)
    5.         {
    6.             var roll = Input.GetAxis("EQ");
    7.             var pitch = Input.GetAxis("WS");
    8.             var yaw = Input.GetAxis("DA");
    9.  
    10.             Vector3 strafe = new Vector3(Input.GetAxis("Strafe") * strafeSpeed * Time.deltaTime, Input.GetAxis("Vertical") * strafeSpeed * Time.deltaTime, 0);
    11.  
    12.             float power = Input.GetAxis("ZX");
    13.  
    14.             //Truespeed controls
    15.  
    16.             if (throttle < maxspeed && throttle > -3)
    17.             {
    18.                 throttle += power;
    19.             }
    20.             if (throttle > maxspeed)
    21.             {
    22.                 throttle = maxspeed;
    23.             }
    24.             if (Input.GetKey("backspace"))
    25.             {
    26.                 throttle = 0;
    27.             }
    28.  
    29.             Vector3 relativeTorque = new Vector3(pitch * turnspeed * Time.deltaTime, yaw * turnspeed * Time.deltaTime, roll * turnspeed * Time.deltaTime);
    30.             Vector3 relativeForceThrottle = new Vector3(0, 0, throttle * speed * Time.deltaTime);
    31.             Vector3 relativeForceStrafe = new Vector3(strafe.x, strafe.y, strafe.z);
    32.             Vector3 relativeForce = relativeForceThrottle + relativeForceStrafe;
    33.          
    34.             // Mass of the ship to compare childrens' mass to. When we hit the ship, it will scale by 1.
    35.             float shipMass = GetComponent<Rigidbody>().mass;
    36.  
    37.             // Force and Torque to apply to all rigidbodies inside ship.
    38.             Vector3 modifiedTorque = new Vector3(relativeTorque.x, relativeTorque.y, relativeTorque.z);
    39.             Vector3 modifiedForce = new Vector3(relativeForce.x, relativeForce.y, relativeForce.z);
    40.  
    41.             // Apply force and torque to all rigidbodies inside ship.
    42.             Rigidbody[] rigidBodyChildren = topOfShip.GetComponentsInChildren<Rigidbody>();
    43.             foreach (Rigidbody childRB in rigidBodyChildren)
    44.             {
    45.                 // Scale force by each child's mass before applying
    46.                 float relativeMass = childRB.mass / shipMass;
    47.                 childRB.AddRelativeTorque(modifiedTorque * relativeMass, ForceMode.Force);
    48.                 childRB.AddRelativeForce(modifiedForce * relativeMass, ForceMode.Force);
    49.             }
    50.         }
     

    Attached Files:

    Last edited: Mar 13, 2015