Good morning, everyone. I’m fairly new to Unity, and I’ve had plenty of fun working through several tutorials so far. I’ve decided to start working on a project of my own, but I’ve run into a bit of a snag.
I’m currently using the rigidbody component on a fighter set in a space scene. The script uses the AddRelativeForce and AddRelativeTorque functions to achieve newtonian-based physics for movement and rotation. While I can manually counter the thrust and rotation on the fighter, I was wondering if there is any way to do this automatically as well. I thought about a system to determine the local x, y and z rotation rate of the object, as well as the translation (strafing) velocity along the local x y and z axis according to the heading of the ship. Unfortunately I have yet to determine if there is a built-in function for finding this information in order to set up any kind of auto-pilot function or something to ease the rotation to zero as the player centers the joystick or releases the button.
I would appreciate any help that can be offered. Does anyone have any ideas? I can certainly post the code when I get home, but I would like to work with some suggestions before I do so - unless it is necessary.
Regards,
-Lance
Okay, here is the code as it currently stands:
/*
For testing purposes, the rigidbody has a mass of 1.
Drag and Angular Drag set to 0. Gravity, kinematic
and freeze rotation set to false. Interpolate set to
none. The intention of setting the drags to zero is
so that counter thrusts can be used to slow things down
at the same rate they were applied via the AddRelativeForce
and AddRelativeTorque values.
Inputs created for this script:
Thrust
Yaw
Pitch
Roll
TransSide
TransVert
TransFor
*/
var rollSpeed : float = 600.0; //rolling rotation speed
var pitchSpeed : float = 600.0; //vertical rotation speed
var yawSpeed : float = 600.0; //horizontal rotation speed
var accelerate : float = 600.0; //main engine thrust
var transZ : float = 600.0; //maneuver thrust forward
var transY : float = 600.0; //maneuver thrust vertical
var transX : float = 600.0; //maneuver thrust horizontal
static var dragOn : boolean = false;
function FixedUpdate(){
var thrust = Input.GetAxis("Thrust") * accelerate;
var yaw = Input.GetAxis("Yaw") * yawSpeed;
var pitch2 = Input.GetAxis("Pitch") * pitchSpeed;
var roll = Input.GetAxis("Roll") * rollSpeed;
var iTransX = Input.GetAxis("TransSide") * transX;
var iTransY = Input.GetAxis("TransVert") * transY;
var iTransZ = Input.GetAxis("TransFor") * transZ;
thrust *= Time.fixedDeltaTime;
yaw *= Time.fixedDeltaTime;
pitch2 *= Time.fixedDeltaTime;
roll *= Time.fixedDeltaTime;
iTransX *= Time.fixedDeltaTime;
iTransY *= Time.fixedDeltaTime;
iTransZ *= Time.fixedDeltaTime;
rigidbody.AddRelativeForce(0,0,thrust); //main thrusters
rigidbody.AddRelativeForce(iTransX, iTransY, iTransZ); //any maneuvering thruster bursts
rigidbody.AddRelativeTorque(pitch2, yaw, roll); //any rotation thruster bursts
//Debug.Log(rigidbody.angularVelocity); //track rotation angle values for local x y and z.
//Debug.Log(rigidbody.velocity); //track velocity rates along local x y and z.
/*
Press O to toggle drags to make things a little easier. Will no longer be newtonian. This is
a sanity shortcut until I can figure out something better.
*/
if(Input.GetKey(KeyCode.O)){
if(!dragOn){
rigidbody.angularDrag = 1.0;
rigidbody.drag = 0.8;
dragOn = true;
}
else
{
rigidbody.angularDrag = 0.0;
rigidbody.drag = 0.0;
dragOn = false;
}
}
}