I am working on an endless runner game. The graphics are fairly smooth except for when my player jumps. I think the jump is creating a difference in speed that causes the slight stutter. Is there a way to smooth that out? The camera is set to follow the x position only. I would like to keep it that way if I can find a suitable solution.
Are you setting the cam position in LateUpdate() or using script execution order to ensure that the camera updates its position after the character does?
I am using LateUpdate. I had never heard of the script execution order. Is that based on coding, placement of items/objects in the hiearchy or something else?
If you’re using LateUpdate, then you don’t need to worry about execution order for this issue. If you want to see how it works, check the documentation– it’s just a little bit of configuration that allows you to arrange any of the scripts you have to execute in a specific order. In a new project, all scripts execute in “Default Time”, and relative order can change each frame. But, you can specify certain scripts to execute before this default, after it, and also when to execute relative to other non-default scripts.
Do you have any videos or images of the effect? Can you share your jumping code and camera follow code?
Here is code related to the jump:
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool onGround;
public float jumpTime;
private float jumpTimeCounter;
onGround = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
if ((Input.GetKey(KeyCode.Space) || Input.GetMouseButtonDown(0)) && onGround)
{
rb.velocity = new Vector2(rb.velocity.x, 20);
stoppedJumping = false;
//jumpSound.Play();
}
if((Input.GetKey (KeyCode.Space) || Input.GetMouseButton(0)) && !stoppedJumping)
{
if(jumpTimeCounter > 0)
{
rb.velocity = new Vector2(rb.velocity.x, 20);
jumpTimeCounter -= Time.deltaTime;
}
}
The camera script is fairly simple maybe too simple but I haven’t had good success with any of the smoothing code yet so I have kept it very simple.
public class CameraController : MonoBehaviour {
public Transform Followplatform;
public Transform Player;
void LateUpdate ()
{
transform.position = new Vector3(Player.position.x, transform.position.y, transform.position.z);
}
}
Here is a link to the video, I just realized the actual video recorder frame rate was low so the real game experience is alot better, but the video does show the stutter movement. I will record again with higher frame if a more accurate video would be helpful. Here is what I have for now.
I can’t see any difference when the character jumps in the video, unfortunately. Sometimes I think I can see it, but then I realize that any stutter I’m seeing is also present when not jumping.
I don’t see anything weird about the code, either, although you should use code tags when posting code to the forums for formatting reasons.
Could you go into more detail about what happens when you jump? Maybe the higher frame rate capture would help, too-- sorry for asking so much.
please post your update, as you have only posted some of the contents of the update and some of the variables.
without seeing your update, fixed update and late update structure it is hard to help.
also it could be another script interacting so please mention everything that is scripted in the scene
Here is the update for the player:
void Update()
{
if(transform.position.x > speedMilestoneCount)
{
speedMilestoneCount += speedIncreaseMilestone;
speedIncreaseMilestone = speedIncreaseMilestone * speedMultiplier;
moveSpeed = moveSpeed * speedMultiplier;
}
rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
onGround = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
if ((Input.GetKey(KeyCode.Space) || Input.GetMouseButtonDown(0)) && onGround)
{
rb.velocity = new Vector2(rb.velocity.x, 20);
stoppedJumping = false;
//jumpSound.Play();
}
if((Input.GetKey (KeyCode.Space) || Input.GetMouseButton(0)) && !stoppedJumping)
{
if(jumpTimeCounter > 0)
{
rb.velocity = new Vector2(rb.velocity.x, 20);
jumpTimeCounter -= Time.deltaTime;
}
}
if(Input.GetKeyUp (KeyCode.Space) || Input.GetMouseButtonUp(0))
{
jumpTimeCounter = 0;
stoppedJumping = true;
}
if(onGround)
{
jumpTimeCounter = jumpTime;
}
myAnimator.SetFloat ("Speed", rb.velocity.x);
myAnimator.SetBool("Grounded", onGround);
}
void OnCollisionEnter2D (Collision2D other)
{
if(other.gameObject.tag == "killbox")
{
theGameManager.RestartGame();
moveSpeed = moveSpeedStore;
speedMilestoneCount = speedMilestoneCountStore;
speedIncreaseMilestone = speedIncreaseMilestoneStore;
deathSound.Play();
}
}
}
Other scripts involved in this game would be the platform generator, platform destroyer, object pooler and score manager.
Here is the higher frame rate capture. Although the app seems to have reduced resolution on that setting. I will try to get a better app for recording this.
Try setting the Interpolate property on the rigid body of the character?
I tried that already before posting. It helped a little but did not clear up the isssue. Thanks.