public Vector2 speed = new Vector2(0,-5);
public float range =1.7f;
void Start () {
GetComponent<Rigidbody2D> ().velocity = speed;
transform.position = new Vector3 (transform.position.x - range * Random.value, transform.position.y, transform.position.z);
}
I also tried :
Translate ( without Rigidbody2D)
Add force ( with Rigidbody2D + is Kinematic Off)
Character controller
Each time i tried with Update(),FixedUpdate() and switching between Time.deltaTime, Time.FixedeltaTime and Time.smoothDeltaTime, Turning VSync On/Off, changing FixedTimestep to differents values (0.0166 for example). I really tried everything i found on forums.
I tested the game on many devices and for some of them the movement looked smooth but for others the jittering was obvious.
So i really want to know, is’t impossible to move an object without jittering or am i missing something.
A kinematic rigidbody is a rigidbody that animates. You don’t really use velocity with this. This rigidbody will NOT be impacted by forces imposed on it by other rigidbodies. This mode is to allow the kinematic body to effect other non kinematic bodies. As long as you update in ‘FixedUpdate’, it will move those objects… as if it’s an unstoppable moving mass through the world. You can technically use the ‘Update’ function as well… but its effect on other rigidbodies won’t be accurate.
A non-kinematic body on the other hand is intended to act with physics. You do NOT directly update the position of it. Instead you apply forces to it with the various ‘AddForce’ functions. When not kinematic you ALWAYS apply forces in the ‘FixedUpdate’ call.
In my case if i use a kinematic rigidbody, the object don’t move even if i apply a transform.postion.
The non-kinematic body with AddForce in method FixedUpdate give me jittering. I tried to modify the FixedTimestep to call FixeUpdate more often but same result.
Edit : transform.translate with a kinematic rigidbody (i removed the velocity) called in FixedUpdate give me also jittering. Or did you mean to use another method except transform.postition and translate to make the object move?
This will give you the smoothest movement, but like Lordofduct said, its effect on other rigidbodies might not be 100% accurate. Also make sure you set Rigidbody2D Interpolate = Interpolate.
public float speed = -5.0f;
public float range = 1.7f;
private Transform tr;
void Awake ()
{
// Store references to save CPU time.
tr = GetComponent <Transform> ();
}
void Update ()
{
Vector3 movement = new Vector3 (tr.position.x - range * Random.value, speed, 0);
tr.Translate (movement * Time.deltaTime);
}
I also think your random range code might be wrong. Since the way you do it makes you always move to the left. The code below will make it move randomly in both directions:
float randomX = Random.Range (tr.position.x - range, tr.position.x + range);
Vector3 movement = new Vector3 (randomX, speed, 0);
Edit: I also hope this random jittering you’ve added to the code is not what you’re complaining about If that’s the case, just remove “- range * Random.value”
I tried your code but it give always the same problem. Just to be sure, i need to be used with Kinematic On and interpolate ? (not sur interpolate or extrapolate are usefull i mean they always get me a worse result).
The jittering is clear on the editor and my android device (and of course i’m not talking about the random range :p) but you’re right about the range always makes me move to the left.
I have static a background, could it be that the problem? also VSync should be On or Off ?
Thanks for your help guys but i really start to think that it’s impossible to correct or i have a problem in my unity editor ( V4.6.2 bu the way).
Oops! I also forgot that the random range was done in start before, so you might actually want this instead. Note: This code runs completely smooth for me.
public float speed = -5.0f;
public float range = 1.7f;
private Transform tr;
void Awake ()
{
// Store references to save CPU time.
tr = GetComponent <Transform> ();
}
void Start ()
{
// Set X position to random range.
float randomX = Random.Range (tr.position.x - range, tr.position.x + range);
tr.position = new Vector3 (randomX, tr.position.y, 0);
}
void Update ()
{
// Move Y position by speed.
tr.Translate (Vector3.up * speed * Time.deltaTime);
}
Interpolate is only really useful for non-kinematic or if you were using FixedUpdate here. Since it helps smooth out physics movement between frames, but no harm in leaving it on anyway. Vsync Should always be off for mobile.
What do you mean by “static background”? As long as the background doesn’t have a collider, it shouldn’t matter. To make sure something else isn’t causing the jittering, I’d create a new project to test just this code.
Well i created a new project i did everything you said and the sphere does move smoothly. That got me thinking maybe it’s the size of my obstacle or his scale how causes the jitters.
I attached a package how containes the obstacle that i want to move. Can you test it and tell me if he moves smoothly?
I opened your sample project in Unity 4.6 and it did seem to have some jittering, but turning off VSync in the Quality Settings fixed this for me. So ensure that VSync is turned off in your project settings. Moving a rigidbody that has multiple child rigidbodies has also caused me issues in the past. So I would advise not doing that whenever possible. You should also only have one rigidbody attached to the parent, and none on the children.
However, I’ve refined the code again to try to eliminate any other possible causes. This new code appears to be the best way to move rigidbodies (according to the manual) If you want them to interact with other rigidbodies correctly. The use of Time.fixedDeltaTime might improve things too.
public Vector2 velocity = new Vector2 (0, -5.0f);
public float range = 1.7f;
private Transform tr;
public Rigidbody2D rb2D;
void Awake ()
{
// Store references to save CPU time.
tr = GetComponent <Transform> ();
rb2D = GetComponent <Rigidbody2D> ();
}
void Start ()
{
// Set X position to random range.
float randomX = Random.Range (tr.position.x - range, tr.position.x + range);
tr.position = new Vector3 (randomX, tr.position.y, 0);
}
void FixedUpdate ()
{
rb2D.MovePosition (rb2D.position + velocity * Time.fixedDeltaTime);
}
With VSync off, this code runs completely smooth in the sample project for me. I’ve attached the updated sample project below for you to try out. If you still can’t get it to run smoothly, then I’m afraid I’m out of ideas
The fact that you find some jitters made me happy, at least i’m not crazy.
So i tested your new code with VSync off, Interpolate , Fixed TimeStep (0.02 / 0.0166 /0.01 ). On some devices ( like Samsung Galaxy Ace 4) , it was really smooth and on others ( like Samsung Galaxy S5) a complete disaster. Actually i noticed that the more powerfull and bigger the screen is, the more jitters are obvious.
Now we’re both out of ideas .
Anyway thank you very much , i really appreciate that you took the time to try to help me.
No problem. The power of the device will have a big impact on the physics performance, but the issue you have sounds vary strange indeed.
If you’re using Unity 4 you could try upgrading to 5 to see if that improves things. You could also try switching to using 3D physics with a fixed Z axis instead. I’ve actually done this for a project because the physX system seems more mature and has more advanced options than the 2D physics.