- Home /
Scripting Halp!! Basic Helictopter Controller
I need to write/edit a script to control a helicopter with super basic movements.
WASD: forward, left, backward, right
Space: lift (it would be nice to have it float a bit when released)
That's it. I have no real working knowledge of creating a script from scratch but I can make adjustments once I have one in place. Anything will help, even a link to something obvious!
This is my first time ever trying to script something in Unity.
Thanks!
Answer by gfvfubb · Mar 06, 2013 at 06:00 AM
I would start with the AngryBots controller and add to it the Unity wiki script for rigidbody spaceship controls since it has a rigidbody up/down/left/right that you could add upward propulsion to. http://wiki.unity3d.com/index.php/ShipControls
http://unitycoder.com/blog/2012/12/15/photon-networking-multiplayer-game/
That describes how to strip down the scene. Once it's stripped check PlayerMoveController and FreeMovementMotor. If you add new controls to PMC and then add corresponding adjustments with rigidbody.AddForce in FMM (replaced with Ship controls) you can get upward propulsion that follows gravity and then you can modify the WASD controls to act more 'helicopter'-ish.
This also provides you with a pre-established framework to re-set animations from the player model.
To get a helicopter from those player controls, you could add a throttle set to W / S and have that AddForce ( character.up , ..etc.). If you google 'banking controls unity' you can see that you can parent a gameobject and add quick banking. You can also add rotational forces with with Torque and connect it to A / D or Q / E or whatever and get a proto-helicopter.
Thanks for the comment. I have been playing around with the assets from angry bots and its really exactly what I was trying to do. Except my axises for the heli gameobject are all fucked so I had to rework the script to get it working. Also today I tried the add force applied to the rigid body and it works great!
Ill post my current script in a while. It's messy but I would like some feedback on my formatting and such.
Thanks again for the help!!
Answer by black pigeon · Mar 04, 2013 at 09:32 AM
do you have animation on your model?
No animations yet. I'm planning to add animations in blender for simple tilt and pitch when it moves forward and side to side. this game is not so much a heli simulator but more of a 3rd person shooter.
Here's my basic script so far. Having problems with lift function. when the copter goes up it stops after a short while and starts falling again. even when i hold down shift it just falls slower...
Any tips would be great. (the vectors were messed up in the .blend so the coordinates seem wrong but it works like it should except for lift.)
var liftSpeed : float = 50.0; var speed : float = -50.0; var rotationSpeed : float = 80.0;
function Start () {
}
function Update () {
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Space))
transform.Translate (0,0, liftSpeed *Time.deltaTime);
var translation : float = Input.GetAxis ("Vertical") * speed;
var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate (translation,0,0);
transform.Rotate (0,0,rotation);
}
I've tried your script, I made a cube...
I dont know what axis that you've used as front, so I use z(+)axis as front of my heli
When I press W it strafes right, S strafes left, A tilts left, and D tilts right
When I press space it moves forward
I'm not using rigidbody to add gravity by the way,
Am I see it wrong? Or is that what you've been making?
I don't think you're wrong. The axises are screwed up for my helicopter game object. I did not align them in blender before importing. I had to adjust the value coordinates within each function to get it to work.
The WASD actually work quite nice and I'm going to add animations to give it a pitch effect. I have added rigid body component and use gravity. As of this writing I reworked the space bar function. Ins$$anonymous$$d it now it adds force to simulate lift.
Thanks for the help btw, I think my first real lesson is to get my axises aligned before asking for help. :P