• 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 CapCorn999 · May 30, 2011 at 03:06 PM · cameralagdelaysmoothingflight sim

how do you add camera lag or delay for flight sim

Hi, I am pretty new to scripting and unity but have done some tutorials etc. I am trying to make a flight sim and have made the movement controller for the plane without too many problems. My problem is that the plane looks fixed in the viewport so I want to add some delay to the camera instead of just parenting it to the plane. I have written this script and attached it to the camera:

Code:

 var targetObject : Transform;
 
 function Update() {
 
 Follow();
 }
 function Follow () {
 
   transform.position = targetObject.position;
   transform.rotation = targetObject.rotation;
   
 }

I have tried using - yield WaitForSeconds(1); - with the script but it just seems to make the camera wait 1 second and jump behind the plane. Can anyone help me add some lag to the script or suggest another way of achieving camera lag? Thanks

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Rodin · May 30, 2011 at 03:59 PM

We will need a way to store the route of the plane for the last second or so, and 'playback' this route to the camera, with some delay.

To get you started, lets create two arrays in which we will store the position and rotation of the plane for each point in time. And a variable controlling how much lag the camera will have (in frames for the time being):

 var prevPositions = Array();
 var prevRotations = Array();
 var cameraLag = 100;    

Then, in the Update function, lets append the current position and rotation to the arrays, and retrieve the position and rotation as it was 100 frames ago:

 function Update() {
     prevPositions.Push(targetObject.position);
     prevRotations.Push(targetObject.rotation);

     if(prevPositions.length > cameraLag) {
         // .Shift() returns and removes the first element in an array
         transform.position = prevPositions.Shift();
         transform.rotation = prevRotations.Shift();
     }
 }

This is not a perfect solution (it is slow and the camera lag is specified in frames, not seconds), but one that is easy to understand and may put you on the right track.

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 AssassinKnifeV2 · Sep 12, 2015 at 07:12 PM 0
Share

Noob question but how to i import that to my game @Rodin

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Camera Movement Smoothing 1 Answer

Unity got Lag/Delay when change scene, How do I Fix it? 2 Answers

FPS Movement Incredibly Laggy 0 Answers

Delay while rotating a camera (doesn't happen in the editor) 1 Answer

Canvas position not in sync with camera 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges