- Home /
2D Camera following Distance
Hey guys,
I am struggling, with a little problem that I have: I started a 2D-Game, where the Camera follows my Character on the X and Y-Axis. But my problem is, the character is to close to the camera and I want adjust a defined distance. I looked on different tutorials, but nothing worked for me so far.
Here is my original following code
var Target : Transform;
public static var gameRunning : boolean = true;
function Update () {
if(gameRunning){
transform.position.x = Target.position.x;
transform.position.y = Target.position.y;
}
}
This is what I tried:
var distance : float = -200f;
var Target : Transform;
public static var gameRunning : boolean = true;
function Update () {
if(gameRunning){
transform.position.x = Target.position.x;
transform.position.y = Target.position.y;
transform.position.z = distance;
}
}
Hope you can help me with that.
To start you really need to give us some more information on what you are trying to accomplish and what problem you are running into. But in the mean time here are just some friendly tips.
I'm assu$$anonymous$$g this code is attached to your camera, but is this a child of another GameObject or a standalone? If this script is attached to a child GameObject then you need to modify transform.localPosition ins$$anonymous$$d. Also, you generally don't set position one value at a time (for the most part these individual values are read only) so it's best practice to do something like this:
`transform.position = Vector3(Target.position.x,Target.position.y,distance)`
I also noticed you are using a gameRunning Boolean to control your Update function. For the most part you want to stay away from booleans that interrupt your update, there are a number of more efficient ways of pausing your game than this. Doing it this way would likely require you to continue checking if the game is running in all your script update functions which just becomes a mess.
Anyway, just some friendly tips. Post some more info on your current problem and I'll see if I can help you out :)
Thanks for your answer.
Well, yeah this script is attached to my main camera and it gets the current position information from my character. I tried to replace my transform with yours, but I have still the same problem.
I don't know if you understood my problem. Basically I want to move the camera in z-position but it is staying still to close to my character.
Well I understand your problem. You are just trying to move your camera closer to this object you are following on X and Y. However, we need to know key pieces of info like "this is an orthographic camera", since at that point moving the camera closer would do nothing. You would ins$$anonymous$$d have to change the Size of the camera ;)
Oh thank you. You just solved my problem, by asking me which camera type i am using. ;) Thanks a lot ;)
No Prob :) I'm just going to type this up as an answer so anyone looking can find it easier.
Answer by ucfblythe83 · Jan 28, 2014 at 05:43 PM
The OP was using a orthographic camera along with his 2D Scene. Moving an orthographic camera only changes position not how objects are viewed. Instead, changing the Size of an orthographic camera will allow you to 'zoom' to the target object.