Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

camera delay or lag for flight sim

Discussion in 'Scripting' started by CapCorn999, May 30, 2011.

  1. CapCorn999

    CapCorn999

    Joined:
    May 30, 2011
    Posts:
    2
    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 (csharp):
    1. var targetObject : Transform;
    2.  
    3. function Update() {
    4.  
    5. Follow();
    6. }
    7. function Follow () {
    8.  
    9.   transform.position = targetObject.position;
    10.   transform.rotation = targetObject.rotation;
    11.  
    12. }
    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
     
  2. fededevi

    fededevi

    Joined:
    Jun 11, 2010
    Posts:
    29
    try this for a very simple smooth follow effect, in FixedUpdate:

    transform.position = transform.position * 0.99 + targetObject.position * 0.01;
     
  3. CapCorn999

    CapCorn999

    Joined:
    May 30, 2011
    Posts:
    2
    Thanks for that but I've managed to fix the problem by making the camera a child of the plane and altering it's local rotation by 10 degrees when you press the keys. Here is the code for anyone who finds this post looking to do somthing similiar (attach script to camera):

    var smooth = 2.0;
    var tiltAngle = 10.0;
    function LateUpdate () {
    var tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle;
    var tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;
    var target = Quaternion.Euler (tiltAroundX, 0, tiltAroundZ);

    transform.localRotation = Quaternion.Slerp(transform.localRotation, target,
    Time.deltaTime * smooth);;
    }
     
  4. RogueMedkit

    RogueMedkit

    Joined:
    Jan 18, 2018
    Posts:
    2
    This works in 2021.3 with a few modifications:

    Code (CSharp):
    1. float smooth = 2f;
    2. float tiltAngle = 10f;
    3. void LateUpdate () {
    4. float tiltAroundZ = Input.GetAxis("Horizontal") * tiltAngle;
    5. float tiltAroundX = Input.GetAxis("Vertical") * tiltAngle;
    6. Quaternion target = Quaternion.Euler (tiltAroundX, 0, tiltAroundZ);
    7. transform.localRotation = Quaternion.Slerp(transform.localRotation, target, Time.deltaTime * smooth);
    8. }
    Also, if you are using a third-person perspective, I would recommend childing the Camera to an empty gameObject centered on the plane, then use this script on the plane. This way, the plane stays in the same spot on the screen while rotating as intended.
     
    Last edited: Dec 21, 2022
  5. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    720
    Alternatively, just use Cinemachine! It’s a very good system, especially if you want many different cameras with complex behaviors.
     
    Kurt-Dekker likes this.