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 problems when using animations and physics

Discussion in 'Scripting' started by Artisiti, Apr 22, 2014.

  1. Artisiti

    Artisiti

    Joined:
    Oct 6, 2013
    Posts:
    26
    Hi there.

    I am making a side scrolling game and I've run into an issue with my camera. The camera is shaking/is jerky. Note that I have the rigid body set to interpolate.

    I do all the physic stuff in FixedUpdate and all the cam stuff in LateUpdate.

    Here is the problem.

    If I have the camera in Late update the scene looks fine but when I move the character the camera renders him jaggy. All other animation stuff (not handled with physics) look fine.

    If I try to move the camera logic to Fixed Update all the effects are reversed. It does not metter if I track the rigidbody or if I track a child transform of it. The effects are the same. Changing the rigidbody interpolation still does not change anything. I hope you can help me.

    PlayerLogic
    Code (csharp):
    1. void FixedUpdate ()
    2.     {
    3.         if (grounded)
    4.         {
    5.             doubleJump = false;
    6.         }
    7.         grounded = Physics.CheckSphere(groundCkeck.position, groundRadius, whatIsGround);
    8.  
    9.  
    10.         rigidbody.velocity = new Vector2 (moveDirection * maxSpeed, rigidbody.velocity.y);
    11.  
    12.         if (moveDirection > 0.0f  !facingRight)
    13.         {
    14.             Flip();
    15.         }
    16.  
    17.         if (moveDirection < 0.0f  facingRight)
    18.         {
    19.             Flip();
    20.         }
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update ()
    25.     {  
    26.            
    27.         if (Input.GetButton ("Fire1")   Time.time > nextFire)
    28.         {   nextFire = Time.time + fireRate;
    29.             anim.Play(attackState);
    30.         }
    31.  
    32.  
    33.         if (moveDirection == 0  grounded)
    34.         {
    35.             anim.SetBool("Idle", true);
    36.         }
    37.         else
    38.         {
    39.             anim.SetBool("Idle", false);
    40.         }
    41.  
    42.  
    43.         moveDirection = Input.GetAxis ("Horizontal");
    44.  
    45.         if (Input.GetButtonDown ("Jump")  (grounded || !doubleJump))
    46.         {
    47.             Debug.Log(rigidbody.velocity);
    48.             rigidbody.velocity += new Vector3(0, jumpHeight, 0) ;
    49.             //rigidbody.AddForce(new Vector2(0, jumpSpeed)); //use this if the above method does not work
    50.  
    51.             anim.SetBool ("Jump", true);
    52.  
    53.             if(!doubleJump  !grounded)
    54.             {
    55.                 doubleJump = true;
    56.             }
    57.         }
    58.         else
    59.         {
    60.             anim.SetBool ("Jump", false);
    61.         }
    62.  
    63.         if(grounded)
    64.         {
    65.             anim.SetFloat ("MoveDirection", Mathf.Abs (moveDirection));
    66.  
    67.         }
    68.  
    69.         else
    70.         {
    71.             anim.SetFloat ("MoveDirection", 0);
    72.  
    73.         }
    74.  
    CameraLogic
    Code (csharp):
    1. void LateUpdate () {
    2.  
    3.         transform.position = new Vector3 (target.position.x, target.position.y, transform.position.z);
    4.    
    5.     }
     
  2. uanmanarmy

    uanmanarmy

    Joined:
    Jan 22, 2014
    Posts:
    25
    You could watch the Unity's 2d Slider video with Mike, they made something like this.
     
  3. Artisiti

    Artisiti

    Joined:
    Oct 6, 2013
    Posts:
    26
    If I remember correctly he is using a static camera (not moving). My problem happens only if the camera moves/follows the player.
     
  4. Artisiti

    Artisiti

    Joined:
    Oct 6, 2013
    Posts:
    26
    Tried to put all the players code into fixed update. Still no progress.

    P.S: the target is an empty GO parented to the player.
     
  5. M-Fatah

    M-Fatah

    Joined:
    Jul 24, 2013
    Posts:
    1
    Hello mate ,
    i've made few sidescroller games with a similar setup for the camera like you did here , but i've never experienced the camera to render the player jaggy like you had

    try to put the camera related code into Update() Method ,, since LateUpdate() is useful for tracking object that moves inside Update()
    http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.LateUpdate.html

    also try to multiply position values with Time.deltaTime :)
     
    Last edited: Apr 23, 2014
  6. Artisiti

    Artisiti

    Joined:
    Oct 6, 2013
    Posts:
    26
    mFatah thanks for the suggestion. The shake is still there >.<