Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question how to make an object follow a non-static object without jittering?

Discussion in 'Scripting' started by godfucker3000, Apr 2, 2022.

  1. godfucker3000

    godfucker3000

    Joined:
    Nov 6, 2020
    Posts:
    5
    Hello everyone, i got tired of not finding a way to make this work so i thought maybe some of you could give me a hand on this

    i have an object A and an object B, the object B is a child of my first person controller's Camera, i have a raycast which i use to select the object A and once the object A is selected, the object A will start following object B and also rotate according to object B's current rotation, it works but i've noticed that when i'm moving my mouse cursor (camera) AND walking, the object A begins to jitter, if i'm only walking (without moving the camera) the object A does not jitter, if i only move the camera(the mouse) without walking, the object A does not jitter.

    this is the code for the object A:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Pickable : MonoBehaviour
    6. {
    7.  
    8.    public Transform pickable_holder;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.        var colider = transform.GetComponent<Collider>();
    14.        colider.enabled = false;
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.        transform.position = Vector3.Lerp(transform.position, pickable_holder.position, Time.deltaTime * 8f);
    21.        transform.rotation = Quaternion.Lerp(transform.rotation, pickable_holder.rotation, Time.deltaTime * 8f);
    22.     }
    23. }
    24.  
    (pickable_holder is the object B)
    here is a video illustrating my problem, object B is the white rectangle, the paper is the object A:



    so this only occurs when my character is in movement which only affects the object B world position and at the same time when i'm moving my camera around which affects the object B world position and rotation, so there's clearly something going on with the position values, as if they were being overwritten or something but i can't figure it out what it is

    thanks to everyone in advance for taking their time to help me sort this out
     
    Last edited: Apr 2, 2022
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    godfucker3000 and Kurt-Dekker like this.
  3. godfucker3000

    godfucker3000

    Joined:
    Nov 6, 2020
    Posts:
    5
    thank you for replying ill check it out
     
  4. godfucker3000

    godfucker3000

    Joined:
    Nov 6, 2020
    Posts:
    5
    so i read the wordpress and i applied some changes to the code, object A does follow object B but it gets fixed at objects B's position and rotation, so the swaying and jittering i previously had are lost, now is there any way to keep the delay/swaying?, i dont know if you watched the video but on the minute 0:16 is where the problem takes place, notice everything was working just fine, when i rotate the camera of the player, the position and rotation of the object A are correctly changing according to object B(white square) would it be because when i'm standing still the pace of the framerate is constant?, no it can't be, because when im walking without rotating the camera no jittering is visible, so i don't believe this is correlated to the framerate
     
    Last edited: Apr 2, 2022
  5. godfucker3000

    godfucker3000

    Joined:
    Nov 6, 2020
    Posts:
    5
    for anyone having a similar issue i moved the code inside the Update function to FixedUpdate and has reduced the jittering considerably but not completely fixed the problem
     
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    I'd start with SmoothDamp and go from there.