Helicopter control script

Hi there,

I have the following code to control my helicopter, but I would like a more sophisticated way, for example the vertical axis for up or down, and the A and Z key for throttle. and the horisontal axis for turning/rudder

Any help perhaps? I am totally new at this.
The code is JavaScript

#pragma strict

var forwardSpeed:float=50;
var turnSpeed:float=2;

function Update () 
{

	var forwardMoveAmount= Input.GetAxis("Vertical")*forwardSpeed;
	var turnAmount= Input.GetAxis("Horizontal")*turnSpeed;
	
	transform.Rotate(0,turnAmount,0);
	
	rigidbody.AddRelativeForce(-forwardMoveAmount,forwardMoveAmount,0);

}

Cheers,

Jakesie

I have played around, and have this, but does not work

#pragma strict

var forwardSpeed:float=50;
var turnSpeed:float=2;
var throttleSpeed:float=2;

function Update () 
{

	var forwardMoveAmount= Input.GetAxis("Vertical")*forwardSpeed;
	var turnAmount= Input.GetAxis("Horizontal")*turnSpeed;
	var throttleAmount= Input.GetKey("z")*throttleSpeed;
	
	
	transform.Rotate(0,turnAmount,forwardMoveAmount*0.01);
	
	rigidbody.AddRelativeForce(throttleAmount,forwardMoveAmount,0);

}

It doesn’t work because GetKey returns a bool.
You could try

var throttleAmount= (Input.GetKey(KeyCode.Z) ? 1 : 0) * throttleSpeed;

though I don’t know if this will work as the short bool is C#, no idea if it works in US
If not, you could make an if/else statement

Just a suggestion for future, if you get an error, you should post it too.

One thing I would suggest is to make your variable names (the floats) the actual names of the control surfaces you want, to make everything easier to visualize. So if it is a realistic helicopter, you need a throttle, elevator, aileron, and rudder. With floats of those names, you can do what you’re doing already - apply forces to the rigidbody using the rigidbody functions.

You may find that adding drag will make everything more controllable, and fun. It will prevent your helicopter from spinning out of control when you apply forces to it. You can find that option in scripting, or in the inspector window under the rigidbody options.

Good luck!

Good morning ,

Thanks for the advice and tips.
@Morning, I’ve tried that, and it give no error, however, the input does not do anything at all.

@Ender, Thanks, I will rewrite the code as suggested. My problem for now is to find out how to use keyboard inputs for example throttle up/down, etc.

Thanks for the help!

Jakesie

How about utilizing the commands from the input manager? How does one do that?

My apologies, I am new and learning quick :roll:

for a key a code like this works

Awesome!! Thanks for the help!!

What am I doing wrong?

cannot get it to work:

#pragma strict

var pitchControl:float=50;
var bankControl:float=2;
var throttleSpeed:float=1;
Input.GetKeyDown("z");

if(Input.GetKeyDown("z")){
var throttleSpeed = 1 ;
}

function Update () 
{

	var forwardMoveAmount= Input.GetAxis("Vertical")*pitchControl;
	var turnAmount= Input.GetAxis("Horizontal")*bankControl;
	var upAmount = Input.GetKeyDown("z")*throttleSpeed ;
	
	
	
	transform.Rotate(0,turnAmount,0);
	
	rigidbody.AddRelativeForce(-forwardMoveAmount,upAmount,0);

}

Well, you have a few things cluttering, remove line 6 and 8 - 10, on line 15 you are using GetAxis, then change line 23 to rigidbody.AddRelativeForce(Vector3.Up*-forwardMoveAmount*throttleSpeed) and change throttleSpeed to a more realistic value to simulate the actual force a heli puts out on itself. I would go into greater detail, if you need it.

Your if statements are outside of Update so they are never called.