- Home /
Improve smooth 2d side scroller camera to look more fluent
Hi,
I am using a very simple linear interp camera with the key lines being
Vector3 newPos = target.position + new Vector3(0, relativeHeigth, -zDistance);
transform.position = Vector3.Lerp(transform.position, newPos, Time.deltaTime * dampSpeed);
(this is from the camera script of the marble game in 5 Unity game examples: C#). This works beatifully for a slow paced game, but in my case velocities are a bit higher (often rigidbody.velocity.magnitude reaches 20 under normal scalings) and when the game is fast my marble doesn't look sharp but its edges seem to be unsteady. If I change the script to a primitive
this.transform.position = new Vector3(target.transform.position.x, this.transform.position.y, this.transform.position.z);
then the edges look much sharper (but obviously the smooth following of the camera is gone).
Any ideas how in the smooth script the unsteadiness of the edges can be approached?
Best, Instability
Have you tried tilting the step for the lerp? like im guessing the dampspeed is a const from the examples, you should try to calculate a step that is relative to the object you're following.
if your camera is perspective, you might try switching to orthographic..
Sorry I don't get your comment, can you expand it? What do you mean by step? Time.deltaTime * dampSpeed? By tilting the step, do you mean adjusting dampSpeed? I tried this but the effect at large velocities doesn't go away, even at very high dampSpeed.
@Instability: You even named his post a comment, why do you answer with an answer to your question? Answers should answer the question. Q&A, you know?
I've converted you answer into a comment.
First post on unityAnswers! Thx bunny (this should be a comment).
Answer by jeffmoretti · Jun 01, 2016 at 04:24 PM
I had a similar problem with scrolling 'smoothness' myself. My code that I had written was as follows (make sure to read the note below after the code):
public GameObject player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 offset = transform.position - player.transform.position;
bool changeInX = false;
bool changeInY = false;
Vector3 newCameraPosition = transform.position;
if (offset.x > 3)
{
changeInX = true;
newCameraPosition.x = (player.transform.position.x + offset.x) - (offset.x - 3);
}
else if (offset.x < -3)
{
changeInX = true;
newCameraPosition.x = (player.transform.position.x + offset.x) - (offset.x + 3);
}
if (offset.y > 2)
{
changeInY = true;
newCameraPosition.y = (player.transform.position.y + offset.y) - (offset.y - 2);
}
else if (offset.y < -2)
{
changeInY = true;
newCameraPosition.y = (player.transform.position.y + offset.y) - (offset.y + 2);
}
if (!changeInX)
{
newCameraPosition.x = transform.position.x;
}
if (!changeInY)
{
newCameraPosition.y = transform.position.y;
}
transform.position = newCameraPosition;
}
The thing that I did which fixed it to smooth scrolling was to change the void Update() function to a void LateUpdate() function. Once I did that, the scrolling was much more smooth! Hope this helps :)
Your answer
Follow this Question
Related Questions
Free Look Camera Pivot 1 Answer
Lock main camera on x-axis 2 Answers
How to get GUI text to appear on screen in 2d? 1 Answer
2d camera movement 1 Answer
Restricting camera to within areas. 2D Adventure Platformer 1 Answer