- Home /
Unity3d : Moving a sprite body causes jitter
Hi guys, I am fairly new to unity development. I want to find out why when I move a sprite object, it is causing very noticeable jitter. I have read quiet some answers on the internet mainly on this community, some say things about rigidbody2d, some about fixed or late update etc etc. But my problem does not seem to be solved by any of those. I just have a sprite object, no rigidbody, just a gameobject with sprite renderer component and a script attached to it in an empty project containing a camera ofcourse. Here is the whole script I am using, you can just copy paste into a cs file named MoveBackground and it will work.
using UnityEngine;
using System.Collections;
public class MoveBackground : MonoBehaviour {
Vector3 pointA;
Vector3 pointB;
float timeRequired;
public float speed = 5;
IEnumerator movingEnum;
float belowEdge;
// Use this for initialization
void Start () {
Camera cam = Camera.main;
float height = 2f * cam.orthographicSize;
belowEdge = cam.transform.position.y - height/2f;
movingEnum = StartMoving ();
initializeAndMoveObject ();
}
// Update is called once per frame
void Update () {
// transform.Translate(0, (-1*speed * Time.deltaTime) / 100, 0);
//this.gameObject.transform.Translate (0f , Time.deltaTime * speed,0f);
}
void MoveToNextPosition()
{
float distance = Mathf.Abs( Mathf.Sqrt (Mathf.Pow ((pointA.x - pointB.x), 2f) + Mathf.Pow ((pointA.y - pointB.y), 2f)));
float time = distance/speed;
timeRequired = time;
movingEnum.MoveNext ();
}
IEnumerator StartMoving()
{
while (true) {
yield return StartCoroutine(MoveObject(transform, pointA, pointB, timeRequired));
}
}
IEnumerator MoveObject(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
{
var i= 0.0f;
var rate= 1.0f/time;
while (i < 1.0f) {
i += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp(startPos, endPos, i);
yield return null;
}
//MovedToTargetPosition();
}
public void initializeAndMoveObject()
{
Vector3 moveTowardsPoint;
SpriteRenderer sR = GetComponent<SpriteRenderer> ();
Vector3 bounds = sR.bounds.extents;
float height = bounds.y * 2f;
moveTowardsPoint = transform.position;
moveTowardsPoint.y = belowEdge - height / 2f;
pointA = transform.position;
pointB = moveTowardsPoint;
MoveToNextPosition ();
}
}
This script moves the object from top to bottom. Also, I dont have an jitter when I run it on my computer in editor. But when I run it on device (iPhone6+) , it causes very very big annoying jitter, constantly, depending on how fast it is moving down.
This is not the only method I have tried to make an object move, I have tried using
transform.Translate(0, (-1*speed * Time.deltaTime) / 100, 0);
in update and fixedupdate to no avail. I have tried using Quad for this purpose. (My gameobject just basically is a Bg that moves down). Piece of code for quad script is
public float speed = -0.5f;
private Vector2 savedOffset;
void Start () {
savedOffset = renderer.sharedMaterial.GetTextureOffset ("_MainTex");
}
void Update () {
float y = Mathf.Repeat ((Time.time * speed), 1);
Vector2 offset = new Vector2 (savedOffset.x, y);
renderer.sharedMaterial.SetTextureOffset ("_MainTex", offset);
}
void OnDisable () {
renderer.sharedMaterial.SetTextureOffset ("_MainTex", savedOffset);
}
If some one can guide me, I would be very thank full to you.
You are doing a lot here! But I guess I don't know the context in which you need all this :)
The code to simply animate the background downward at a constant velocity can be simplified to:
using UnityEngine;
using System.Collections;
public class $$anonymous$$oveBackground : $$anonymous$$onoBehaviour {
// Update is called once per frame
Vector3 speed = new Vector3 (0f, -1f,0f);
Rigidbody2D rb;
void FixedUpdate () {
transform.position = transform.position+ Time.deltaTime* speed;
}
}
If this is still causing jitter, then something else is happening. Perhaps setting targetFrameRate to something lower and disabling vsync ( Project Settings->Quality-VSync Count) might help?
void Awake()
{
Application.targetFrameRate = 30;
}
However, I too sometimes get unexpected jitter. So I'd love to see you get to the bottom of this.
Actually the default framerate for iOS devices is 30. In our project we've set that to 60 to get smoothing into movements.
Answer by Abdul91 · Mar 10, 2015 at 09:39 AM
I am putting this in answer as davient said in his comment. This issue for me was solved by setting the targetFrameRate in the code to 60 for iPhone6+(setting 30 didnt work, setting -1 didnt work), any value above 60 works too but below 60 was causing jittering. So all I changed was, I had to execute this statement once my app started (Anywhere in the code will work but preferably Awake, in my scenario it didnt had to be in Awake). I actually solved this issue with help of a friend, but I really appreciate the reply davient. Thnx :)
Application.targetFrameRate = 60;
Unity documentation reference : http://docs.unity3d.com/ScriptReference/Application-targetFrameRate.html
Just a side note, not all devices require 60 framerate.
Your answer
Follow this Question
Related Questions
Add Cyrillic to TexMeshPro?,Add krill to TexMeshPro? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
,How to stop jump animtion 1 Answer