- Home /
Jerky/jittery object movement
Hi
I'm working on a C# script that enables a player to change an object it's position to the left or the right, depending on which side of the screen the player is touching.
However with the following code I'm getting a lot of random jerky/jittery movement at complete random times while moving the object to the left or the right.
Things I've done so far:
I've tried both using FixedUpdate and Update along with trying to add Time.deltaTime or other methods of the Time class in the transform.translate code parts
Changed the application/game framerate to a max of 120fps
Enabled/disabled the rigidbody2D component
Interpolate, extrapolate rigidbody2D component
Played with the VSync settings.
Current setup:
Unity version: 4.3.4f1 free
There is one main camera that doesn't move
The object itself sits in another object for pivot rotating reasons (the objects rotates to one side or the other while moving along the same x axis as it would without rotation)
Using a Nexus 5 with Android 4.4.2 (so it must have plenty of power to generate decent framerates)
Link to image: link
I think it has something to do with the time interval FixedUpdate not generating fixed times during movement of the object that causes the jittery.
C# Touch (move) script:
using UnityEngine;
using System.Collections;
public class Touch : MonoBehaviour {
public GameObject player;
private double halfScreen;
// Use this for initialization
void Start () {
halfScreen = Screen.width / 2.0;
}
// Update is called once per frame
void FixedUpdate () {
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
{
Vector2 touchPosition = Input.GetTouch(0).position;
//Check if it is left or right?
if(touchPosition.x < halfScreen){
player.transform.Translate(Vector3.left * 0.8f);
} else if (touchPosition.x > halfScreen) {
player.transform.Translate(Vector3.right * 0.8f);
}
}
}
}
I'm really desperate for some help, I've been searching for the answer for a day or 2 now. I've read numerous forums but none of the solutions I found helped me out.
Please also note that I'm a new user to Unity3D
Thanks in advance for the help!
Use :
LateUpdate()
Late update updates after all other calculations and since you're accesing touch in Update() the system needs to calculate position at it was before and where it is now.
I'm wondering if it has to do with checking input in FixedUpdate(). How about checking for input in Update() and setting a boolean variable. Then, in FixedUpdate(), check the status of the variable and apply the translation if it is true.
Answer by monserboy · Feb 18, 2014 at 06:22 PM
Combining both the comments from DajBuzi and Nanobrain did the trick. I used a LateUpdate along with a boolean check.
This is the code for future reference:
using UnityEngine;
using System.Collections;
public class Touch : MonoBehaviour {
public GameObject player;
private double halfScreen;
private bool touched = false;
// Use this for initialization
void Start () {
halfScreen = Screen.width / 2.0;
}
void Update(){
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary){
touched = true;
} else {
touched = false;
}
}
// Update is called once per frame
void LateUpdate () {
if(touched == true){
Vector2 touchPosition = Input.GetTouch(0).position;
//Check if it is left or right?
if(touchPosition.x < halfScreen){
player.transform.Translate(Vector3.left * 0.08f);
} else if (touchPosition.x > halfScreen) {
player.transform.Translate(Vector3.right * 0.08f);
}
}
}
}
Thanks guys!!