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

Can't get smooth 2d movement on one object using transform.position

Discussion in 'Scripting' started by Caelorum, Apr 29, 2015.

  1. Caelorum

    Caelorum

    Joined:
    Feb 6, 2014
    Posts:
    32
    So I have 3 objects doing a transform.position type of thing. One object follows the player, mirroring his every move. The camera follows the player, doing the exact thing. Then a rectangle follows the object following the player. The object following the object following the player, is horrible stuttery. And I don't now why.

    Here is the script for it.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Shield : MonoBehaviour
    5. {
    6.     GameObject shieldBase;
    7.     Transform rotation;
    8.     Vector2 shieldPos;
    9.     void Awake()
    10.     {
    11.         shieldBase = GameObject.FindGameObjectWithTag ("Follow");
    12.     }
    13.     void Update()
    14.     {
    15.         ShieldMovement ();
    16.     }
    17.  
    18.     void ShieldMovement()
    19.     {
    20.         rotation = shieldBase.transform;
    21.         shieldPos = shieldBase.transform.position;
    22.         this.transform.rotation = rotation.rotation;
    23.         this.transform.position = shieldPos;
    24.     }
    25.  
    26. }
    Incredibly simple code. All it needs to do is change it's position, to the other objects' position every frame. Yet it can't seem to do that. The object it needs to follow, is moving perfectly fine. Yet this object can't seem to keep up for some reason.

    It has a rigidbody2d with gravity and mass set as low as possible, i've tried using the interpolate, does nothing. I've tried setting to kinematic. I've removed the rigidbody entirely. Still constant stuttering. Even on the build & run version.

    I've noticed this problem a lot in unity, objects just not being able to move without irritating stutter.
     
  2. Caelorum

    Caelorum

    Joined:
    Feb 6, 2014
    Posts:
    32
    Apparently it had something to do with script execution order. I guess one script was moving too late for the other script. They must have been conflicting in movement.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    For this reason, anytime script X depends on script Y being finished, you should put script X's functionality into LateUpdate instead of Update.
     
    JoeStrout and Caelorum like this.