AddRelativeTorque not working

Hi, I am working on a helicopter control script that you can quickly attach to any object you want to be a helicopter, but I’ve come into one problem: the lines that say AddRelativeForce do add the force but it’s not adding it in the right directions. For example, i want my heli to angle downwards (rotating on the z axis) so it can look at the ground, and it works if i’m at like a global 45 degree angle but if i’m at like a global 180 degrees, it will try to angle on the z, get stuck on…something, and then rotate on the x axis.

var throttle : float;
var rightT : float;
var leftT : float;
var forwardT : float;
var backT : float;
var RightTurnSpeed : float;
var LeftTurnSpeed : float;
var heightLimit : float;


function Update () {

leftT = rightT * -1;
Nthrottle = throttle * -1;
backT = forwardT * -1;
LeftTurnSpeed = RightTurnSpeed * -1;
transform.rotation.x = 0;



	if (Input.GetKey("left shift")  gameObject.transform.position.y < heightLimit) {
		rigidbody.AddForce (transform.up * throttle);
		}
		else
		{
			rigidbody.AddForce (transform.up * 15500);
		}
		
	if (Input.GetKey("d")) {
		rigidbody.AddForce (transform.forward * rightT); 
		rigidbody.AddRelativeTorque (2000, 0, 0);
		}
		
	if (Input.GetKey("a")) {
		rigidbody.AddForce (transform.forward * leftT);
		rigidbody.AddRelativeTorque (-2000, 0, 0);
		}
		
	if (Input.GetKey("left ctrl")) {
		rigidbody.AddForce (transform.up * Nthrottle);
		}
		
	if (Input.GetKey("w")) {
		rigidbody.AddForce (transform.right * backT);
		}
		
	if (Input.GetKey("s")) {
		rigidbody.AddForce (transform.right * forwardT);
		}
		
	if (Input.GetKey("right")) {
		rigidbody.AddRelativeTorque (0, RightTurnSpeed, 0);
		}
		
	if (Input.GetKey("left")) {
		rigidbody.AddRelativeTorque (0, LeftTurnSpeed, 0);
		}
		
	if (Input.GetKey("up")) {
		rigidbody.AddRelativeTorque (0, 0, 40000);
		}
	
	if (Input.GetKey("down")) {
		rigidbody.AddRelativeTorque (0, 0, -40000);
		}
		
	if (gameObject.transform.rotation.z > 4) {
		rigidbody.AddRelativeTorque (0, 0, -160000);
		}
		
	if (gameObject.transform.rotation.z < -4) {
		rigidbody.AddRelativeTorque (0, 0, 160000);
		}
		
	if (gameObject.transform.rotation.x < -6) {
		rigidbody.AddRelativeTorque (4000, 0, 0);
		}
	
	if (gameObject.transform.rotation.x > 6) {
		rigidbody.AddRelativeTorque (-4000, 0, 0 );
		}
		
		
}

I have tried removing the angle correction parts, but that does not do anything.

bump

Bump

What you probably want to do there, is divide that script into 2 parts: Input Handling, and Simulation.

What you have now, is code that will only apply forces when a key is being held down… If no key is held down, no forces are being applied at all. That might not be what you want, since a helicopter does need forces acting on it all the time. But the biggest problem is that that stack of 'if’s can easiliy get convoluted and impossible to unravel, and isolating a problem becomes a problem wholly on it’s own.

So, the Input Handling part would read the keypresses, and change the appropriate values.

Then, the simulation part will apply forces, based on those values.

This way, you always have a valid set of control data for the simulation, and you separate the keys from the end actions, making it easier to spot problems.

Another tip, try not to use so many constant values… define public variables, (like pitchMaxForce or throttleMaxForce), and use them instead. This way, you can fiddle with them in Unity’s inspector as you play, and tune it to your liking.

Hope this helps

Cheers

Thank you for the advice, i will try that :slight_smile: