Why is my character movement jerky/stuttry/jettry?
Hi , I'm trying to make a 2D game and I'm trying to nail the movement down, but no matter what I use it's always as I described it in the title, until I tumbled upon a characterController2D script made by Prime31 and when I used it the movement became buttery smooth in the right way I wanted to do it, I tried to understand the gentleman's code but I'm not an expert so im still in the process of understanding it
this is a video to show the difference between my movement code (bottom cube) and prim31's script (top cube)
here is prime31's code on Github, and for me I used all possible ways of movement that I know of, through the rigidbody and or through the transform
so if I can get some insight on how to get same results without having to use prime31's code or at least how I could improve the jerkiness seen in video
pardon my English , thank you
you need to share your code for us to help you, but it feels like you are using fixedupdate/update wrong, update is called every frame so its where you should do the movement, if you are using rigidbodys or any other physics use the fixedupdate.
Answer by KingKong320 · Apr 09, 2019 at 06:51 AM
@xxmariofer this is the script attached on camera
public Transform leftSideLimt, rightSideLimit;
public GameObject FollowTargetOBJ;
public float FollowSpeed;
//Paralax layers
public GameObject[] BackgroundROOTS;
private bool PlayerJustDied;
void Start ()
{
FollowTargetOBJ = GameObject.FindGameObjectWithTag ("Player");
}
void FixedUpdate ()
{
if (PlayerJustDied == false && FollowTargetOBJ.transform.position.x > leftSideLimt.position.x && FollowTargetOBJ.transform.position.x < rightSideLimit.position.x) {
//Smoothly Follow Target
Vector3 PositionBefore = transform.position;
Vector3 NewPosition = Vector3.Lerp (transform.position, FollowTargetOBJ.transform.position, FollowSpeed * Time.deltaTime);
transform.position = new Vector3 (NewPosition.x + 2.5f, NewPosition.y + 1.3f, transform.position.z);
//Paralax layer movement
Vector3 CameraMovementAmount = PositionBefore - this.transform.position;
BackgroundROOTS [0].transform.Translate (-CameraMovementAmount * 0.8f);
}
}
i set interpolate to interpolate..it started jerks,but when i set interpolate to none camera follows fine but characters movement is not smooth.Please help me out
first change the FixedUpdate to the Update, i thought your issue was the player not being smooth not the camera
i have done that but that did not fix in update :(.... And my character is moving smoothly but not the camera is following it smoothly...i have attached this script on empty gameobject and that empty gameobject contains 2 cameras in scene,,one is following background and the other is following character.
ok try changing this 2 lines
Vector3 NewPosition = Vector3.Lerp (transform.position, FollowTargetOBJ.transform.position, FollowSpeed * Time.deltaTime);
transform.position = new Vector3 (NewPosition.x + 2.5f, NewPosition.y + 1.3f, transform.position.z);
to this
Vector3 NewPosition = Vector3.Lerp (transform.position, FollowTargetOBJ.transform.position + new Vector3(2.5f, 1.3f, 0.0f), FollowSpeed * Time.deltaTime);
transform.position = new Vector3 (NewPosition.x, NewPosition.y, transform.position.z);
Answer by zakkaiokenx10 · Apr 09, 2019 at 07:18 AM
Please use a cinemachine 2d camera and make it follow the player. That will help you better than hard coding a new camera, as well as giving you features you'll enjoy..
Your answer
Follow this Question
Related Questions
2D plaformer, Can bug me into wall 2 Answers
MoveTowards is curving for no reason 0 Answers
having CharacterController movement in fixedupdate causes jerky movement on camera 0 Answers
Bug on Character movement 0 Answers
Walk around square platform 0 Answers