Ah, that actually helped to remove the errors in my speed gauge, although I think I didn’t explain fully what my problem is, I have a script(heliFlightScript) which has a rotor variable (rotor_velocity) that represents the speed of the copter’s rotor. What I need to do is to connect the rotor_velocity from the heliFlightScript to the Velocity HUD script that is in this thread.
My heliFlightScript is referenced from a helicopter tutorial, which helped me to animate and control the helicopter.
var heli_Mrotor_GameObject : GameObject; // (Main Rotor Object)
var heli_Trotor_GameObject : GameObject; // (Tail Rotor Object)
//(The min/max, multipliers, true/false settings of the code)
var max_Rotor_Force : float = 22241.1081; // newtons
static var max_Rotor_Velocity : float = 5200; // degrees per second (max Rotation speed per second of the Main Rotor)
private var rotor_Velocity : float = 0.0; // value between 0 and 1
private var rotor_Rotation : float = 0.0; // degrees... used for animating rotors
var max_tail_Rotor_Force : float = 15000.0; // newtons
var max_Tail_Rotor_Velocity : float = 2200.0; // degrees per second (max Rotation speed per second of the Tail Rotor)
private var tail_Rotor_Velocity : float = 0.0; // value between 0 and 1
private var tail_Rotor_Rotation : float = 0.0; // degrees... used for animating rotors
var forward_Rotor_Torque_Multiplier : float = 0.5; // multiplier for control input (Used to include torque in the flight of the helicopter)
var sideways_Rotor_Torque_Multiplier : float = 0.5; // multiplier for control input
static var heli_Mrotor_Active : boolean = true; // boolean for determining if a prop is active (self-explainatory)
static var heli_Trotor_Active : boolean = true; // boolean for determining if a prop is active
//(If I am correct, FixedUpdate is a code updater to the scripts running in the simulation, getting past the frame rate and updating itself to allow the helicopter
// to fly consistently)
function FixedUpdate () {
// First we must compute the torque values that are applied to the helicopter by the propellers. The "Control Torque" is used to simulate
// the variable angle of the blades on a helicopter and the "Torque Value" is the final sum of the torque from the engine attached to the
// main rotor, and the torque applied by the tail rotor. (A vector to allow the helicopter to "tilt" in mid-air just like real flight. The
// multiplier is to further increase the torque as the helicopter turns further until it stops turning. Input.getaxis is a button input)
var torqueValue : Vector3;
var controlTorque : Vector3 = Vector3( Input.GetAxis( "Vertical" ) * forward_Rotor_Torque_Multiplier, 1.0, -Input.GetAxis( "Horizontal2" ) * sideways_Rotor_Torque_Multiplier );
// Now check if the main rotor is active, if it is, then add it's torque to the "Torque Value", and apply the forces to the body of the
// helicopter. (From above, if the Rotor activeness is activated, then calculate the speed/velocity of the rotor travelling)
if (heli_Mrotor_Active == true ) {
torqueValue += (controlTorque * max_Rotor_Force * rotor_Velocity);
// Now the force of the prop is applied. The main rotor applies a force direclty related to the maximum force of the prop and the
// prop velocity (a value from 0 to 1)
rigidbody.AddRelativeForce( Vector3.up * max_Rotor_Force * rotor_Velocity );
// This is simple code to help stabilize the helicopter. It essentially pulls the body back towards neutral when it is at an angle to
// prevent it from tumbling in the air. ( To prevent flipping the helicopter when it tilts)
if ( Vector3.Angle( Vector3.up, transform.up ) < 80 ) {
transform.rotation = Quaternion.Slerp( transform.rotation, Quaternion.Euler( 0, transform.rotation.eulerAngles.y, 0 ), Time.deltaTime * rotor_Velocity * 2 );
}
}
// Now we check to make sure the tail rotor is active, if it is, we add it's force to the "Torque Value"
if ( heli_Trotor_Active == true ) {
torqueValue -= (Vector3.up * max_tail_Rotor_Force * tail_Rotor_Velocity);
}
// And finally, apply the torques to the body of the helicopter.
rigidbody.AddRelativeTorque( torqueValue );
}
function Update () {
// This line simply changes the pitch of the attached audio emitter to match the speed of the main rotor. (linked from audio)
audio.pitch = rotor_Velocity;
// Now we animate the rotors, simply by setting their rotation to an increasing value multiplied by the helicopter body's rotation.
if ( heli_Mrotor_Active == true ) {
heli_Mrotor_GameObject.transform.rotation = transform.rotation * Quaternion.Euler( 0, rotor_Rotation, 0 );
}
if ( heli_Trotor_Active == true ) {
heli_Trotor_GameObject.transform.rotation = transform.rotation * Quaternion.Euler( 0, 0, tail_Rotor_Rotation );
}
// this just increases the rotation value for the animation of the rotors. (Extends the rotation?)
rotor_Rotation += max_Rotor_Velocity * rotor_Velocity * Time.deltaTime;
tail_Rotor_Rotation += max_Tail_Rotor_Velocity * rotor_Velocity * Time.deltaTime;
// here we find the velocity required to keep the helicopter level. With the rotors at this speed, all forces on the helicopter cancel
// each other out and it should hover as-is. (To keep the helicopter stable and afloat)
var hover_Rotor_Velocity = (rigidbody.mass * Mathf.Abs( Physics.gravity.y ) / max_Rotor_Force);
var hover_Tail_Rotor_Velocity = (max_Rotor_Force * rotor_Velocity) / max_tail_Rotor_Force;
// Now check if the player is applying any throttle control input, if they are, then increase or decrease the prop velocity, otherwise,
// slowly LERP the rotor speed to the neutral speed. The tail rotor velocity is set to the neutral speed plus the player horizontal input.
// Because the torque applied by the main rotor is directly proportional to the velocity of the main rotor and the velocity of the tail rotor,
// so when the tail rotor velocity decreases, the body of the helicopter rotates. (If function is the velocity acceleration by holding down the
// throttle button, Lerp is to return the heli velocity to it's neutral speed if the throttle button isnt held or used)
if ( Input.GetAxis( "Vertical2" ) != 0.0 ) {
rotor_Velocity += Input.GetAxis( "Vertical2" ) * 0.0015;
}else{
rotor_Velocity = Mathf.Lerp( rotor_Velocity, hover_Rotor_Velocity, Time.deltaTime * Time.deltaTime * 5 );
}
tail_Rotor_Velocity = hover_Tail_Rotor_Velocity - Input.GetAxis( "Horizontal" );
// now we set velocity limits. The multiplier for rotor velocity is fixed to a range between 0 and 1. You can limit the tail rotor velocity
// too, but this makes it more difficult to balance the helicopter variables so that the helicopter will fly well. (So as not to go over or under the speed of the helicopter
// to extreme measures)
if ( rotor_Velocity > 1.0 ) {
rotor_Velocity = 1.0;
}else if ( rotor_Velocity < 0.0 ) {
rotor_Velocity = 0.0;
}
}
The tutorial comes with a HUD that uses textures for the frame-by-frame calculation, but I feel that its unrealistic without putting alot of textures and I preferred a scripted gauge for the HUD.
helicopter_throttle = player_gameobject.GetComponent( "Helicopter_Script" ).rotor_Velocity; // now set the helicopter
// throttle value to the
// velocity specified in the
// main helicopter script.
// now we draw the GUI objects...
GUI.Label( Rect( 0, 128, 128, 128 ), throttle_texture[ helicopter_throttle * 10 ] ); // now we draw the throttle texture
// based on what the helicopter script
// gives us (the 10 is in there because
// there are 10 frames to the gauge
// animation).
This is the code that connects the helicopter script to the HUD script, which I’m trying to do the same by implementing it to the scripted gauge, but to no avail. How do I proceed from this?