- Home /
Rotate cam with object, smooth boost and brake
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);
}
Script for camera:
var target : Transform;
var distance = 10.0;
var height = 2.0;
var rotationDamping = 3.0;
function LateUpdate () {
if (!target)
return;
wantedRotationAngleSide = target.eulerAngles.y;
currentRotationAngleSide = transform.eulerAngles.y;
wantedRotationAngleUp = target.eulerAngles.x;
currentRotationAngleUp = transform.eulerAngles.x;
currentRotationAngleSide = Mathf.LerpAngle(currentRotationAngleSide, wantedRotationAngleSide, rotationDamping * Time.deltaTime);
currentRotationAngleUp = Mathf.LerpAngle(currentRotationAngleUp, wantedRotationAngleUp, rotationDamping * Time.deltaTime);
currentRotation = Quaternion.Euler(currentRotationAngleUp, currentRotationAngleSide, 0);
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
transform.LookAt(target);
transform.position += transform.up * height;
}
Answer by adrenak · Feb 14, 2012 at 12:00 PM
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.
I Hope this helped!
Answer by svist_o2 · Feb 14, 2012 at 11:12 PM
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);
}
Yes. If you want to stop the plane when the button is not press, then click on the plane to which you has added rigidbody and in the inspector you will see the rigidbody submenu. The rigidbody submenu will show you different components of the object such mass, drag etc. Put the value of drag and angulardrag to say 0.4. You dont really have to make changes in the script that you have to stop the plane, just put a value of drag and angulardrag.
Answer by svist_o2 · Feb 15, 2012 at 10:20 AM
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);
}
Problem solved! Ins$$anonymous$$d rigidbody.AddTorque, use rigidbody.AddRelativeTorque. It works!
Your answer
Follow this Question
Related Questions
Camera Script - RtS style, rotation issues 0 Answers
Move the camera 2 Answers
Why this script dont work properly? any ide to fix it? 2 Answers
MoveTowards problem 1 Answer
Airplane follower camera script 1 Answer