- Home /
Jerky Camera or Object motion
First I would like to say Hello to everyone. I am new to this Forum, and relatively new to Unity. I hope to find the solution to my problem which has given me quite some headache. I am trying to make a 3D space shooter. The flying object is supposed to orientate itself by using the transform.LookAt
function and accelerate using the rigidbody.AddRelativeForce
. For now I have the camera following the Vessel by simply parenting it to it. It works great only that the movement is a bit jerky at times, it lags mostly noticeable when turning. (the script is attached to the Vessel)
function Update()
{
var scr2world: Ray;
scr2world = Camera.main.ScreenPointToRay(Input.mousePosition);
var vel = transform.position + transform.forward*4;
var focus = Vector3.MoveTowards(vel,scr2world.GetPoint(80),Time.deltaTime * 10);
transform.LookAt(focus);
Debug.DrawRay(scr2world.origin,scr2world.direction*50,Color.yellow);
Debug.DrawLine(transform.localPosition,scr2world.GetPoint(80),Color.red);
Debug.DrawLine(transform.localPosition,focus,Color.black);
}
The Rays and Lines I drew to verify the coordinates. I expect the black line to align itself with the red. It does but when I comment out transform.LookAt(focus);
it remains at Vector3(vel)
and vibrates. I have tired Lerp,Slerp,MoveTowards,smoothDamp. But ran into the same problems or others. Is my methode wrong to achieve the desired motion? Or is my code missing something? Is the jerky motion generated by the Vessles motion or the cameras motion?
If that script is attached to the vessel then as it doesn't affect the camera's position any jerkiness must be due to the movement of the vessel. (Presu$$anonymous$$g nothing else moves the camera).
But is looks to me like the vessel isn't actually moving - you never seem to change its position; well not in that code at least.
I would guess at vibration being to do with the ray being relative to mouse coordinates and not quite getting the exact same result every time - especially when multiplied by 80.
Thank you for you answer, The camera is for now simply a child of the vessel, although I have tried a self written camera follow script using Vector3.Slerp or Unitys standard assets script smooth follow. So far all seem to share the jerky motion problem. The vessel is accelerated using another script
rigidbody.AddRelativeForce (0,0,velZ*velZb); //velZb stands for my boost factor
if (Input.Get$$anonymous$$ey("x") && velZ < 520)
{
velZ += 40;
rigidbody.drag = stDragZ; //stDragZ stand for my standard Drag value
}
I see what you mean, but the vibration occurs even when the vessel is still. When I would expect the black line to be drawn in towards the specified point on the ray, in my case 80? Are there any suggestions to use a different methode to eventually achieve the same? I would really like to keep the LookAt function or emulate it. and keep the rigidbody.AddRelativeForce because it generates a nice Force Vector calculation.
Answer by whydoidoit · May 26, 2012 at 05:29 PM
So your problem is due to the fact that you are multiplying your focus position calculation by Time.deltaTime. This will be different on every call to the Update function, causing your focus position to fluctuate slightly. Then you are doing a LookAt which is also moving the camera very slightly.
Not sure why you need that Time.deltaTime as you are not actually moving anything here (your physics is doing that). This therefore seems like a calculation that should be at a fixed position - drop the Time.deltaTime and replace it with a constant (0.02 for the physics step etc).
Thank you for your great help. You have gotten to the root of the fluctuation, I took your advice and got rid of the Time.deltaTime and the fluctuation is gone. The black line tries to align itself to the red. Depending on how large the constant is that you suggested deter$$anonymous$$es how far it aligns itself with the red. I assume thats normal since the constant represents the speed, it simply shows how far it makes it in one frame, considering that the Update function is called every frame. Although the jerky motion when flying persists. Now that the fluctuation is ruled out as the source of the problem. I believe it to be the fact that the Ray direction from the camera drives the the vessels orientation which drives the Cameras orientation which ultimately changes the Ray direction. Its like a loop that cant keep up. The Question is I need the camera to follow the vessel and want the ray direction to give the Vector3 for the Vessel to look at. Any suggestions on how to achieve that?
First thought, and it's just off the top of my head, but try putting that code in LateUpdate() ins$$anonymous$$d, so that the physics and everything have moved and moved the camera first.
YES !!! Definately the right direction. Although LateUpdate() didnt work. Ins$$anonymous$$d I tried the FixedUpdate(). which completely removes the jerky motion. Thank you for your Great Help and quick responses. I have another problem which is caused by the same code but unrelated to the jerky motion. Since im new to this forum I ask you. Should I make another post?
Your answer
Follow this Question
Related Questions
How to make an object move towards an object at a constant pace, no matter how fast the object is? 0 Answers
Look at wont update? 2 Answers
transform.LookAt overrides rigidbody rotation 0 Answers
Slow down with multiple enmies using LookAt 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers