I can’t get it fixed.
The player almost climbs out of the rage of the main camera and then I want to move the camera to “the new” height.
I have the main camera in a separate empty gameObject. And it follows the player on the x position. But I just want to move the camera once to get it to the new height. And of course when it climbs down I want the camera move to the lower level height.
Put an invisible collider trigger at the top of the screen. Then when the player experiences a collision event with the trigger then change the camera Y axis position to the new height.
I would use something along the lines of if player is above max height lerp camera.y towards new height then the other way if below.
I dont have a way to test this but it should be close. Let me know if this helps.
//Attach player transform here
var player : Transform;
//Distance from end of screen to move camera
var moveDistance : int;
//Attached to camera
function Update()
{
//Convert player position to screen position
var playerPos : Vector3;
playerPos = camera.WorldToScreenPoint(Vector3(0,player.position.y,0);
//Check to see if player is close to top of screen.
if(playerPos.y > Screen.height-moveDistance){
MoveCameraUp(camera.ScreenToWorldPoint(Vector3(0,Screen.height-moveDistance,0));
}
}
funciton MoveCameraUp(newHeight : Vector3)
{
while(transform.position.y < newHeight.y){
transform.position.y = Mathf.Lerp(transform.position.y, newHeight.y,transform.position.y/newheight.y);
yield;
}
}
Performance-wise isn’t it better to rely on a collision event, rather than using the Update method?
In this case, probably not. He’s probably already updating the camera every frame while checking the player position. There’s no way adding colliders is going to be more efficient than simply checking the value of two floats you’ve already retrieved.
Agreed that this may not be something needed in the update but for quick post I went with that. I would check it only when the player is not grounded so I could tell if I need to move the cam up or down.
Hi BeforeTheLight,
It a good start to fix the problem… But it not totally there yet.
It looks that it is working but the moveDistance variable is being ignored. I changed the Vector3 to Vector2’s because i’m working in 2D space.
Here is my code:
#pragma strict
internal var thePlayer:GameObject;
internal var newHeight:Vector2;
var moveDistance:int;
function Start () {
//set the player GameObject
thePlayer = GameObject.Find("Omar");
//set the first time the camera
transform.position.y = thePlayer.transform.position.y;
}
function Update () {
transform.position.x = thePlayer.transform.position.x;
//Convert player position to screen position
var playerPos : Vector2;
playerPos = camera.WorldToScreenPoint(Vector2(0,thePlayer.transform.position.y));
//Check to see if player is close to top of screen.
if(playerPos.y > Screen.height-moveDistance){
MoveCameraUp(camera.ScreenToWorldPoint(Vector2(0,Screen.height-moveDistance)));
}
}
function MoveCameraUp(newHeight:Vector2){
while(transform.position.y < newHeight.y){
transform.position.y = Mathf.Lerp(transform.position.y, newHeight.y,4* Time.deltaTime);
yield;
}
}
I don’t know why the moveDistance is ignored but the player almost disappear on the top and then the camera goes to the middle of the screen. Close but no cigar. cough![]()
Ow and it only works for up… But that’s the if statement… I know.
Yeah I only did the up direction figured you could do the down if you wanted to use this implementation just flip the variables around to go down.
Screen to world looks for a Vector3 as does world to screen so I am not sure how you arent getting a error from that unless 2d gives it a default z of 0.
I have not used the 2d engine sorry for that but my interperation is everything is aligned on the Z axis then sorted by layer. So your Z would be 0 or what ever your base for the 2d would be.
What did you set the distance to?
This is going to be pixels on screen not unity units. So if your screen is 800 tall - 10 is very close to the top. You may want to try view point to world point and world point to view point this will give a normalized number between 0-1 so then you could say playerPos > .8 move cam or you would have to come up with an equation for screen percentage to stick with screen point. EX Screen.height - (Screen.height*.2); would give the top 20% of the screen.
Hope this helps and doesn’t hurt. When I get home I will try this out.
Ok got home created a 2d project and tested. This works.
#pragma strict
//Attach player transform here
var player : Transform;
//Distance from end of screen to move camera
var moveDistance : float;//I used 0.7 here
//Attached to camera
function Update()
{
//Convert player position to view position
var playerPos : Vector3;
playerPos = camera.WorldToViewportPoint(Vector3(0,player.position.y,0));
//Check to see if player is close to top of screen.
if(playerPos.y > moveDistance){
MoveCameraUp(camera.ViewportToWorldPoint(Vector3(0,moveDistance,0)));
}else if(playerPos.y < 1-moveDistance){
MoveCameraDown(camera.ViewportToWorldPoint(Vector3(0,1-moveDistance,0)));
}
}
function MoveCameraUp(newHeight : Vector3)
{
while(transform.position.y != newHeight.y){
transform.position.y = Mathf.MoveTowards(transform.position.y, newHeight.y,4*Time.deltaTime);
yield;
}
}
function MoveCameraDown(newHeight : Vector3)
{
while(transform.position.y != newHeight.y){
transform.position.y = Mathf.MoveTowards(transform.position.y, newHeight.y, 4*Time.deltaTime);
yield;
}
}
I tried it but it jitters… Especially when I walk down again… ![]()