- Home /
2D camera that tracks player after running short distance
Okay, I am making a 2d sidescroller and I am having some trouble with the camera. I can make it so the camera moves along with the player, but that isn't exactly what I want.
I use the same code to move the camera(based on the current speed, acceleration, and top speed) that I use to move the player. I understand why the camera acts the way it does, but I don't know how to make it act the way I want. I have tried making the camera track slightly slower than the player, but while I know this would be good for an auto-scroller, this is not really what I want. I want the player to run a short distance AND THEN for the camera to start tracking.
for example, there would sort of be a box within screen that tells the camera to start tracking.
This way the player could run back and forth along the screen(to a certain point) without the camera moving at all. This is most common in older sidescrolling games, and I am having a hard time replicating it. Something like what I mean can be found in Super Mario World.
I suppose I just need an idea of how to do this, NOT the actual script or code.
THanks in advance!
Answer by robertbu · Aug 29, 2014 at 05:26 PM
Something simple like this on the camera should do the job:
#pragma strict
public var player : Transform;
public var speed = 2.0;
function Update() {
var viewportPos = camera.WorldToViewportPoint(player.position);
if (viewportPos.x > 0.75) {
transform.Translate(speed * Time.deltaTime, 0,0);
}
if (viewportPos.x < 0.25) {
transform.Translate(-speed * Time.deltaTime, 0,0);
}
if (viewportPos.y > 0.75) {
transform.Translate(0, speed * Time.deltaTime, 0);
}
if (viewportPos.y < 0.25) {
transform.Translate(0, -speed * Time.deltaTime, 0);
}
}
This code uses Viewport coordinates. They start in the lower left at (0,0) and go to (1,1) for all screen resolutions/orientations. This code keeps the pivot point of the player in the middle 1/2 of the screen. You can adjust the 0.25 and 0.75 as appropriate to your game.
Ahhh, I did not know these existed! That is exactly what I needed. I am doing it in C# but I think I can convert it well enough.
Is there a benefit to using update() in this case over lateupdate(), I did a bit of research and I am a little confused. I don't have an update() in my camera script, just the lateupdate() because I thought that was what I was supposed to do. is this a case where there is a "proper" way to do it, but doesn't make much of a difference otherwise?
Thanks so much for the help and the reply!
LateUpdate() is a better place for this code. The issue is that if the camera Update() is called first, then the Update() for the player, you can get some player jitter. Putting in LateUpdate() solves this potential problem. Not much to convert to C# here, but if you have issues, let me know.
Your answer
![](https://koobas.hobune.stream/wayback/20220613161811im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Cinemachine Confiner 2D shift my camera view 0 Answers
How to destroy a projectile when it leaves any side of the camera view on a 2D game? 1 Answer
how to get the lowest right corner of the camera(and highest left) in units? 1 Answer
2.5D Platformer Camera follow character Y axis but not for every jump? 1 Answer