Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by svist_o2 · Feb 14, 2012 at 11:17 AM · camerarotationmoveairplane

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;    
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image svist_o2 · Feb 14, 2012 at 06:58 PM 0
Share

Thank you! I think it starts to work.

avatar image
0

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);
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image adrenak · Feb 15, 2012 at 04:48 AM 0
Share

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.

avatar image
0

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);
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image svist_o2 · Feb 15, 2012 at 11:37 AM 0
Share

Problem solved! Ins$$anonymous$$d rigidbody.AddTorque, use rigidbody.AddRelativeTorque. It works!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges