- Home /
Intense Lag/Stutter when 3d cube is moving.
Why does it lag/stutter so much when I'm moving my 3d cube downwards? It's just a simple 3d cube. I've also tried FixedUpdate and Update but I prefer FixedUpdate since in Update, its speed changes drastically depending on the lag, while in FixedUpdate, the speed is constant, but the lag is much greater.
This is the code I'm using right now. Is there something in it that's making my cube lag? My scene only contains a cube and a light, and while it doesn't lag that much on my desktop, it lags really hard on my Samsung Galaxy:
using UnityEngine;
using System.Collections;
public class fall : MonoBehaviour {
public static float speed;
Vector3 move;
void Awake () {
move = new Vector3(0, -9.0f, 0);
speed = 4.0f;
}
// Update is called once per frame
void FixedUpdate () {
move = new Vector3(transform.position.x, -9.0f, transform.position.z);
transform.position = Vector3.MoveTowards(transform.position, move, Time.smoothDeltaTime * speed);
}
}
}
I've also tried Rigidbody.MovePosition and Transform.Translate, but it still lags very hard.
Do you have a rigid body on the game object? If yes, remove it if you don't make use of rigid body simulation for the object. If you need it somehow, make the RB kinematic so it won't be influenced by the simulation. Another reason could be that your rendering frame rate and simulation frame rate are different. Then you could try to enable interpolation/extrapolation on the rigid body or move the translation code into Update() ins$$anonymous$$d of FixedUpdate().
Yeah I do, I need it though, so I'll try setting it to $$anonymous$$inematic. Thanks!
EDIT: It still lags, even with the rigidbody removed.
I also have the same exact problem as this guy: http://answers.unity3d.com/questions/709900/movement-in-unity-is-lagging-every-few-seconds-or.html
Use Update, not FixedUpdate.
Time.deltaTime should be smooth enough.
I had a situation once where I though a character was bobbing around frantically. It actually turned out that the character was smooth as silk and it was the camera which was bobbing.
Answer by hexagonius · Feb 15, 2015 at 03:30 PM
There's two ways to get smoothed visuals:
Always use Rigidbodies on GameObjects that move. Everything else is considered static and expensive when moved anyway.
Manipulate your Transforms in Update() since it's frame based and make the Rigidbody kinematic. You won't get proper collision though.
Manipulate your velocity in FixedUpdate() and use interpolation mode on your Rigidbody component without being kinematic. Since FixedUpdate() is a different loop than Update and the Rigidbody needs to get a chance to know where it's going to be during a render call, it can only predict that when it has a velocity.
Your answer
Follow this Question
Related Questions
2D Background Stop Moving on BoundingBox Exit? 2 Answers
Setting target position in Vector3.MoveTowards 2 Answers
how can do click then move on a gameobject? 1 Answer
Move object with finger swipe 0 Answers
follow along the XZ plane? 0 Answers