- Home /
Moving Camera only when player crosses a point
I was thinking about making a platformer, but I don't want the camera to move with player, I only want it to move, when player crosses the screen border, so you kinda get that feeling that you move between levels even though you are in the same location (examples : Zelda, Pokemon).
I tried to do this myself, wrote a small script, but my problem is that I still do not understand how to use Transform, transform and (G)(g)ameObject stuff.
Here's the code itself
#pragma strict
var player : Transform;
var cam : GameObject;
function Update () {
if (player.position.x > Screen.width) {
cam.transform.position.x += Screen.width;
}
if (player.position.x < -Screen.width) {
cam.transform.position.x -= Screen.width;
}
}
Please help guys, that's the only problem I've got so far
Answer by andylatham · Mar 02, 2015 at 07:56 PM
What you're doing in principle works, but you're just not updating the camera position properly. You can't assign a value to it like any other variable, you have to assign a vector to it.
So instead of writing this:
cam.transform.position.x += Screen.width;
write this:
cam.transform.position = new Vector2 (cam.transform.position.x += Screen.width, cam.transform.position.y);
Depending on what you're doing, you may need Vector3 rather than Vector2, in which case you'd need to specify the z-position too. There may be better ways to do it, but that's the way that I know! :)
You forgot the "equals" in there
cam.transform.position = new Vector2 (cam.transform.position.x + Screen.width, cam.transform.position.y);
Another pesky problem you have to tackle is that Screen.width
is in pixels (and depends on the screen size of a mobile device for example), whereas your character is moving in world units. You can get the screen size in world units with this method. Then just use that width ins$$anonymous$$d of Screen.width (just the first lines of code in the answer)
Okay, so, I combined all the stuff on this topic, that you guys wrote with my script and it works! the only problem now is that it works too much, it constantly goes left and right, will try and fix
and yeah, it doesn't draw a thing, but everything else works right
Answer by hexagonius · Mar 02, 2015 at 05:36 PM
Transform.position is in units and Screen.width is in pixels. Also Screen.width does not have a position somewhere.
You need to transform the right screen edge into world space:
if (player.position.x > Camera.main.ScreenToWorldPoint(new Vector3(Screen.width,0,0)).x) {
Vector3 newPos = Camera.main.transform.position;
newPos.x += Camera.ScreenToWorldPoint(new Vector3(Screen.width,0,0)).x;
Camera.main.transform.position = newPos;
}
thanks for the answer! now unity says, that I need to insert a semicolon after Camera.main when defining Vector3 I tried to define Camera.main to cam, but that didn't work
I changed the parameters for Camera.ScreenToWorldPoint from float to Vector3.
Your answer
Follow this Question
Related Questions
Camera Scripting Help (Sims Style) 0 Answers
Moving Question 1 Answer
Fixed camera on rails. 2 Answers
Making my look script turn smoothly 0 Answers