I am working on a flight sim, and am trying to rotate the throttle gameobject to match the thrust. Pressing the spacebar adds thrust, pressing “b” reduces thrust. Full thrust is achieved at 145 on x axis. Zero thrust at 0 on x axis. I’ve run out of ideas. Any help would be appreciated.
// if (Input.GetKey(KeyCode.B))
// {
// throttleModelCurrentRotation = new Vector3(Mathf.LerpAngle(throttleModelCurrentRotation.x, minThrottleRotation.x, Time.deltaTime), 0, 0);
// throttleModel.transform.eulerAngles = throttleModelCurrentRotation;
// }
//
// if (Input.GetKey(KeyCode.Space))
// {
// throttleModelCurrentRotation = new Vector3(Mathf.LerpAngle(throttleModelCurrentRotation.x, maxThrottleRotation.x, Time.deltaTime), 0, 0);
// throttleModel.transform.eulerAngles = throttleModelCurrentRotation;
// }
if (Input.GetButton("Thrust") && Input.GetAxisRaw("Thrust") > 0)
{
throttleModelCurrentRotation = new Vector3(Mathf.LerpAngle(throttleModelCurrentRotation.x, maxThrottleRotation.x, Time.deltaTime), 0, 0);
}
else if (Input.GetButton("Thrust") && Input.GetAxisRaw("Thrust") < 0)
{
throttleModelCurrentRotation = new Vector3 (0f,0f,0f);//new Vector3(Mathf.LerpAngle(throttleModelCurrentRotation.x, -1f, Time.deltaTime), 0, 0);
}
throttleModel.transform.eulerAngles = throttleModelCurrentRotation;
With this code, the throttle moves forward, but it doesn’t move back to 0.
Mark
Use AddForce to get propulsion. Using Lerp you are just doing a force move on direct transform values. All Vector3 and Quaternion should to be in Fixed Update. To simulate the force from your inputs, you can use this pseudo code as reference:
public Rigidbody targetVehicle; //The object that we want to apply force to move and control it.
//Using rigidbody we will can apply forces and change values on Vector3 and Quaternion properties.
float stabilityAxis; //Stability Axis helps to Axis come back to zero value.
float wantedAxis; //Wanted Axis are the axis that we want to reach.
float currentAxis; //Current Axis are the axis that are right now on Axis Input.
float smoothAxis; //Smooth the movement of our Axis Input.
float velocityAxis; //The speed that will do the axis movement.
void FixedUpdate(){
AxisControl();
targetVehicle.AddForce(Vector3.up * upForce * currentAxis);
//Here we apply force to our object using force instead of Lerp.
targetVehicle.rotation = Quaternion.Euler(new Vector3(0, currentAxis, 0)); }
//Here we apply the Axis Input values on AddForce to create propulsion and on Quaternion to create direction movement. Look how current Axis are on parameters, change it to see diferente effects.
void AxisControl(); {
if(Input.GetAxis(“Horizontal”) <= 0) { wantedAxis += stabilityAxis; }
//This get the negative value from Axis input and store the value on Wanted Axis.
else if(Input.GetAxis(“Horizontal”) >= 0) { wantedAxis -= stabilityAxis;}
//This get the positive value from Axis input and store the value on Wanted Axis.
else if(Input.GetAxis(“Horizontal”) == 0) { wantedAxis == stabilityAxis; }
//If no Axis input is being applyed, zero the values in Wanted Axis.
wantedAxis = Mathf.Clamp(wantedAxis, -50, 50);
//Now we limit the value that Axis can reach on positive or negative sides. Use // on this line code to remove all move restriction.
currentAxis = Mathf.SmoothDamp(currentAxis, wantedAxis, ref velocityAxis, smoothAxis;}}
//Now we will change our current Axis transform position value while we change our Axis input values. The stored currentAxis values will be applyed now at our throttle code in Fixed Update section.
}}