- Home /
Sprite Background Lags with Camera Script
Hi Guys,
I'm new to Unity3D and Game development/Programming. I've been following some tutorials on how to make an RPG on Unity (gamesplusjames on YouTube). When I run my game, I can see some sort of render lag on the background when I Move/Stop moving.
Essentially what is happening, when I move the map blurs and when I stop moving, the map is trying to catch up to itself (from what I can see). You can see an example(sorry video is downside, but you get the point): HERE
If you look at the grass as I move it blurs (which I guess is normal since when you move motion blur does happen) but when I stop moving, you can see the background image tries to catch up to the player position.
Here is my camera follow code in case it might be this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour {
public GameObject followTarget;
private Vector3 targetPos;
public float moveSpeed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
targetPos = new Vector3(followTarget.transform.position.x, followTarget.transform.position.y, transform.position.z);
transform.position = Vector3.Lerp(transform.position, targetPos, moveSpeed * Time.deltaTime);
}
}
By default does Unity render only what you see on the screen? Or do I need to create some sort of code on my camera script to render only what you see? I am guessing it could be something like this?
Any advise, tips, and assistance would be much appreciated. :) I am using Unity 2017.3
I have tried turning off Anti-aliasing in quality settings. If I place my main camera object onto my player object, i get no lag on the background. Which leads me to believe it's something on my script?
Thanks, Mark.D
I did some further testing and if I child the "$$anonymous$$ain Camera" to my player, this issue doesn't occur. So this must have something to do with my camera controller script...
public class PlayerController : $$anonymous$$onoBehaviour {
public float moveSpeed;
private Animator anim;
private bool player$$anonymous$$oving;
private Vector2 last$$anonymous$$ove;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
player$$anonymous$$oving = false;
if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
{
transform.Translate(new Vector3 (Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime,0f,0f));
player$$anonymous$$oving = true;
last$$anonymous$$ove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
}
if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
{
transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * moveSpeed * Time.deltaTime, 0f));
player$$anonymous$$oving = true;
last$$anonymous$$ove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
}
anim.SetFloat("$$anonymous$$oveX", Input.GetAxisRaw("Horizontal"));
anim.SetFloat("$$anonymous$$oveY", Input.GetAxisRaw("Vertical"));
anim.SetBool("Player$$anonymous$$oving", player$$anonymous$$oving);
anim.SetFloat("Last$$anonymous$$oveX", last$$anonymous$$ove.x);
anim.SetFloat("Last$$anonymous$$oveY", last$$anonymous$$ove.y);
}
}
Hey guys,
After some more testing, I found that if the moveSpeed of the camera is to low (below 20) the background renders slow Anything above 20 seems to do the trick, but if I do that I can't add some slack to the camera follow and will follow it more static like. I would like to be able to move a few pixels before the camera starts to follow.