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);
}
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);
}
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.
#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.