Hello! Just sorry for my bad English.
I make airplane. I am novice to Java. I wrote the code for the control of the aircraft, it works. For camera taken as a basis script for a WOWCamera.
But there are three problems:
1 - I can not figure out how to make a smooth acceleration and braking of the aircraft.
2 - How to get the camera rotate along with the plane Z-axis (roll) tried transform.Rotate, but did not work.
3 - Рow to get the camera distance themselves with increasing speed?
Please help solve these problems, or point me to my mistakes.
Thank you.
Script for Airplane:
var Speed = 50;
function Update () {
//Roll
var rotateAirplane = 3;
if (Input.GetKey(KeyCode.LeftArrow)) {
transform.Rotate(0,0,rotateAirplane);
}
else if (Input.GetKey(KeyCode.RightArrow))
transform.Rotate(0,0,-rotateAirplane);
//Constant Speed
transform.Translate(Vector3.forward * Time.deltaTime * Speed * 2);
//Boost-Brake
if (Input.GetKey(KeyCode.W)) {
transform.Translate(Vector3.forward * 3);
}
else if (Input.GetKey(KeyCode.S))
transform.Translate(Vector3.back);
//Yaw
var yawAirplane = 2;
if (Input.GetKey(KeyCode.D)) {
transform.Rotate(0,yawAirplane,0);
}
else if (Input.GetKey(KeyCode.A))
transform.Rotate(0,-yawAirplane,0);
//Pitch
var pitchAirplane = 3;
if (Input.GetKey(KeyCode.UpArrow)) {
transform.Rotate(pitchAirplane,0,0);
}
else if (Input.GetKey(KeyCode.DownArrow))
transform.Rotate(-pitchAirplane,0,0);
1.
I had used a similar approach for a helicopter a while back. Initially, I also used transform to change the position and rotation and in the end the result was not good. Instead I would suggest you to make the airplane into a rigidbody and use force and torque for motion.
This makes the motion look much more realistic as it is physics based. Otherwise you have to keep track of time, like how long the user has been pressing the forward button so that you can make the airplane accelerate. You can set some values for drag and angulardrag that will do the job of decelerating the plane when the user is not pressing the button. Force and torque are independent of time, hence this, I my opinion would be a better approach .
3. Do you mean that as the velocity increases, you want the camera to move further away form the plane to give a zooming out effect for a better sense of speed? For that you can use the fieldofview component of the camera in the scene. Access the velocity of the airplane and increase or decrease the fieldofview value accordingly to the do zooming, it will look exactly as if the camera’s distance is increase and decreasing.
vatsalAtTrifekt thank you for your help! With rigidbodi really everything works smoothly. But I have not has understood how to stop an object when a key is not pressed without pressing another key?
Sorry if too bored, but I’m a novice, and I very much want to learn.
I would be grateful for any response.
That’s part of the script that I wrote:
private var rotateAmount : Vector3 = Vector3 ( 10, 0, 0);
function FixedUpdate () {
//Roll
if (Input.GetKey(KeyCode.LeftArrow)) {
rigidbody.AddTorque(Vector3.forward * 3 + rotateAmount * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.RightArrow))
rigidbody.AddTorque(Vector3.back * 3 + rotateAmount * Time.deltaTime);
}
I begin to understand how it works rigidbody. Airplane is moving as they should, but he moves and turns only on the global axes, that is, if the airplane rotated 180 °, then all the management is not working properly. And my question is, how to get the script to work in the local axes? The help I’ve read about TransformDirection, tried to make a TransformDirection, did not work or am I doing something wrong.
Here’s the resulting script:
var Speed = 50;
private var Amount : Vector3 = Vector3 ( 10, 0, 0);
function FixedUpdate () {
//Roll
if (Input.GetKey(KeyCode.LeftArrow)) {
rigidbody.AddTorque(Vector3.forward * 2 + Amount * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.RightArrow))
rigidbody.AddTorque(Vector3.back * 2 + Amount * Time.deltaTime);
//CurentSpeed
transform.Translate(Vector3.forward * Time.deltaTime * Speed);
//Boost-Brake
if (Input.GetKey(KeyCode.W)) {
rigidbody.velocity = Vector3(0,0,70);
}
else if (Input.GetKey(KeyCode.S))
rigidbody.velocity = Vector3(0,0,-20);
//Yaw
if (Input.GetKey(KeyCode.D)) {
rigidbody.AddTorque(Vector3.up * 2 + Amount * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.A))
rigidbody.AddTorque(Vector3.down * 2 + Amount * Time.deltaTime);
//Pitch
if (Input.GetKey(KeyCode.UpArrow)) {
rigidbody.AddTorque(Vector3.right * 2 + Amount * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.DownArrow))
rigidbody.AddTorque(Vector3.left * 2 + Amount * Time.deltaTime);
}