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

Jittering movement in my 3D snake game

Discussion in 'Scripting' started by xxfelixlangerxx, May 2, 2020.

  1. xxfelixlangerxx

    xxfelixlangerxx

    Joined:
    Feb 6, 2019
    Posts:
    31
    Hey everyone,

    I am experiencing jittery movement in my 3D snake clone.

    This is the code:
    Code (CSharp):
    1.  
    2. //movement
    3.     private Rigidbody rb;
    4.     public float force;
    5.     public float turnspeed;
    6.  
    7.     //spawning
    8.     public int spawndelay;
    9.     public GameObject segment;
    10.     private List<Vector3> lastpos = new List<Vector3>();
    11.     private List<Quaternion> lastrotation = new List<Quaternion>();
    12.     private List<GameObject> segments = new List<GameObject>();
    13.  
    14.  
    15.  
    16.  
    17.     private int i = 1;
    18.  
    19.     private void Start()
    20.     {
    21.         rb = GetComponent<Rigidbody>();
    22.         StartCoroutine(spawnseg());
    23.     }
    24.  
    25.     private void Update()
    26.     {
    27.         lastpos.Insert(0, transform.position);
    28.         lastrotation.Insert(0, transform.rotation);
    29.  
    30.         Move();
    31.  
    32.         UpdateSegments();
    33.  
    34.     }
    35.  
    36.     //removed move function(uses rb.addforce and rb.addrelativetorque
    37.  
    38.     private void FixedUpdate()
    39.     {
    40.         rb.velocity = (force * transform.forward * Time.deltaTime);
    41.     }
    42.  
    43.     public void addnewSegment()
    44.     {
    45.         GameObject newsegment = Instantiate(segment, lastpos[spawndelay * i], lastrotation[spawndelay * i]);
    46.         segments.Add(newsegment);
    47.         newsegment.GetComponent<SegmentScript>().spawnint = spawndelay * i;
    48.         i++;
    49.  
    50.     }
    51.  
    52.     void UpdateSegments()
    53.     {
    54.  
    55.         foreach(GameObject intsegment in segments)
    56.         {
    57.             intsegment.transform.position = lastpos[intsegment.GetComponent<SegmentScript>().spawnint];
    58.             intsegment.transform.rotation = lastrotation[intsegment.GetComponent<SegmentScript>().spawnint];
    59.         }
    60.     }
    61.  
    62.     private IEnumerator spawnseg()
    63.     {
    64.         yield return new WaitForSeconds(3f);
    65.         addnewSegment();
    66.         StartCoroutine(spawnseg());
    67.     }
    Basiclly i save all the last positions in a list, then have the objects just move to the next spot in the list.
    However the segments seem to be jittering around in their place even when going straight.

    Thanks
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    There's a lot going on here, let's fix a few things one by one and see what happens.

    First off, I notice you're incorporating Time.deltaTime in your FixedUpdate() method. This could certainly be contributing to the jitteriness as FixedUpdate already runs at a simulated fixed interval. Time.deltaTime will change on a frame-by-frame basis, whereas FixedUpdate() may run once, zero, or multiple times per frame. I also suggest reducing the value of your "force" variable at the same time to around its current value divided by ~60 or it will have a very high velocity when you remove Time.deltaTime.
     
  3. xxfelixlangerxx

    xxfelixlangerxx

    Joined:
    Feb 6, 2019
    Posts:
    31
    Thanks,
    it kinda worked. I found out that when simply moving with transform.position += force * transform.forward; in the update function there is not jittering when using force however i have again high amout of jittering
     
    Last edited: May 2, 2020