Throttle setting based on angle of thruster

Hey all,

I’ve been banging my head for a while trying to get a working system that converts player input into thruster throttle setting. The trick is the user places them at runtime so I can’t know head if a thruster is used for pitch/roll/yaw.

So here are just a few things I’ve tried. Many of them work for some rotations, but not others. I can’t find a single method that works for all possible rotations of both the thruster and input.

All thrusters can be used from -1 (100% reverse) to 1 (100% forward). And since it’s a rotational force, a thruster’s forward vector can match or be rotated 90 degrees and still apply some rotation force correctly.

Thanks for any help! I’ve been going nuts trying to figure this out and just can’t wrap my head around it.

  Vector3 inputDir = transform.TransformDirection(Input.GetAxis("horz"), Input.GetAxis("Vert"), Input.GetAxis("Roll"));

        for (int i = 0; i < thrusterArray.Length; i++)
        {

            //snip, different methods I've tried

            // method 1 
            float throttle = Vector3.Dot(thrusterArray[i].transform.forward, inputDir);

            // method 2
            Vector3 projected = Vector3.Project(inputDir, thrusterArray[i].transform.forward);
            float throttle = Vector3.Dot(thrusterArray[i].transform.forward, projected);

            // method 3
            Vector3 rotatedInput = thrusterArray[i].transform.rotation * inputDir;
            float throttle = Vector3.Dot(thrusterArray[i].transform.forward, rotatedInput);

            // method 4
            Vector3 thrusterCoMDir = Vector3.Normalize(transform.InverseTransformPoint(thrusterArray[i].transform.position)
                                        - _rigidbody.centerOfMass);
            Vector3 projected = Vector3.Project(thrusterCoMDir, thrusterArray[i].transform.forward);
            float throttle = Vector3.Dot(projected, inputDir);

            // finally apply the forces
            _rigidbody.AddForceAtPosition(thrusterArray[i].transform.forward * Throttle * scaleForce, thrusterArray[i].transform.position);
        }

Is this supposed to be like kerbal space program where you create your own rockets and aircraft? Really hard to give any suggestions without knowing what you want to achieve with this an what’s not behaving as intended.

Like, what’s the purpose of using transform.InverseTransformDirection? If you wanted the input to be relative to the rotation of the transform, wouldn;t you use transform.TransformDirection instead?

Yes, but with submarine thruster so it can work both forward and reverse. And yep, that was an auto complete mistake in the code paste only. I’m using non inverse TransformDirection for the input.

The problem is any of the above code example, they work for some rotations of the thruster but then for others it produces incorrect results. Meaning a thruster will fire when it shouldn’t, or not when it should. :slight_smile:

Also as the the thrusters and input vectors cross the center of mass for the craft, the values should flip since this is a rotational force applied. Pitch/Roll/Yaw so it rotates the rigidbody.