Search Unity

Camera movement decrease my fps

Discussion in 'Editor & General Support' started by HeyNau, May 1, 2016.

  1. HeyNau

    HeyNau

    Joined:
    Aug 26, 2015
    Posts:
    10
    Hello!

    Im working on a project that should always have a camera focusing to the Oso and Pinguino. When the Pinguina is near, it will focus between Oso and Pinguina, moving horizontally and moving to zooming out (Pinguino will be always in the middle).

    In another script I created an hability that makes Oso jump:
    - RigidOso.AddForce (5, 10, 0, ForceMode.VelocityChange;

    For any reasson, when Oso jumps and begins his fall, this character (only this character) falls in slow motion, like a FPS rate issue (only at Oso).

    Looking for the problem I found that, only if I remove the last line of this code the issue will be solved:
    - Camara.transform.position = new Vector3 (PosicionCamaraX, 3, ValorFadeZ);

    But I need this camera movement.

    Another clue:

    At Oso's Rigidbody, I've frozen Z position (Deep in a 2D game). If I let it free, it works. But if I lock it by code:
    - Update {Oso.transform.position = new Vector3(Oso.transform.position.x, Oso.transform.position.y, 3.0f); }
    It breaks again.
    Looking at Oso's Z-coord. I see that jumping moves him a little bit in Z axis, I dont know why but probably this angle is what slows it's fall. Gravity is y=-9.81.


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System;
    5.  
    6. public class CamaraMove : MonoBehaviour {
    7.  
    8.     public GameObject Camara;
    9.     public GameObject Pinguino;
    10.     public GameObject Oso;
    11.     public GameObject Pinguina;
    12.     public float ValorFadeZ;    //Pinguina-oso
    13.     public float ValorZ;    //Pinguino-oso
    14.     public float DistanciaPinguina;
    15.     public float PosicionPinguino;
    16.     public float PosicionOso;
    17.     public float PosicionPinguina;
    18.     public float PosicionFadePinguina;
    19.     public float ContadorFadePinguina = 0;    //Contador para suavizar el cambio de camara
    20.     public float PosicionCamaraX;
    21.  
    22.  
    23.     void Start () {
    24.  
    25.     }
    26.  
    27.     void Update () {
    28.  
    29.         PosicionPinguino = Pinguino.transform.position.x;
    30.         PosicionPinguina = Pinguina.transform.position.x;
    31.         PosicionOso = Oso.transform.position.x;
    32.  
    33.         DistanciaPinguina = PosicionPinguina - PosicionPinguino;
    34.         ValorFadeZ = PosicionOso - PosicionFadePinguina;
    35.         ValorZ = PosicionOso - PosicionPinguino;
    36.  
    37.         if (DistanciaPinguina < 20) {    //PINGUINA CERCA
    38.             if (ContadorFadePinguina < 1.0f) {
    39.                 ContadorFadePinguina += 0.5f * Time.deltaTime;
    40.                 PosicionFadePinguina = Mathf.Lerp (PosicionPinguino, PosicionPinguina, ContadorFadePinguina);
    41.                 ValorFadeZ = Mathf.Lerp (ValorZ, ValorFadeZ, ContadorFadePinguina);
    42.             }
    43.         } else {
    44.             PosicionFadePinguina = PosicionPinguino;
    45.             ValorFadeZ = ValorZ;
    46.         }
    47.         if (ValorFadeZ > -10f) {
    48.             ValorFadeZ = -10f;
    49.         }
    50.         else if (ValorFadeZ <= -10f) {
    51.             ValorFadeZ = ValorFadeZ / 2f + (-10f * 1f / 2f);
    52.         }
    53.         PosicionCamaraX = (PosicionFadePinguina + PosicionOso) / 2;
    54.         Camara.transform.position = new Vector3 (PosicionCamaraX, 3, ValorFadeZ);
    55.     }
    56. }
    57.  
    58.  
    59.  
    60.  
     
    Last edited: May 1, 2016
  2. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    495
    Try changing Interpolate value in Rigidibody to Interpolateor or Extrapolate
     
  3. HeyNau

    HeyNau

    Joined:
    Aug 26, 2015
    Posts:
    10
    It didn't work. I just tried with "None", "Interpolate" and "extrapolate".
    Extrapolate looks a little bit different, like having milimetric rollbacks while Oso falls.
     
  4. idurvesh

    idurvesh

    Joined:
    Jun 9, 2014
    Posts:
    495
    Try keeping it at Extrapolate and move Oso falls movement to fixedupdate while keeping camera movement at LateUpdate
     
  5. HeyNau

    HeyNau

    Joined:
    Aug 26, 2015
    Posts:
    10
    I'm trying with some combinations of "LateUpdate" and "FixedUpdate" and no one works =(