Question by
adjmpw · Aug 20, 2016 at 09:39 AM ·
cameracamera-movement
Getting player position respective to camera.
I am trying to move camera along with the player using beow code
void FixedUpdate () {
float worldScreenHeight = Camera.main.orthographicSize * 2.0f;
float worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;
float xaxisLimit = 1*worldScreenWidth/5.0f;
float yaxisLimit = 1*worldScreenHeight/4.0f;
float halfScreen = (transform.position.y - player.transform.position.y);
Debug.Log (halfScreen);
float posX = Mathf.SmoothDamp(transform.position.x, player.transform.position.x + xaxisLimit, ref velocity.x,smoothTimeX);
//float posY = Mathf.SmoothDamp(transform.position.y, player.transform.position.y + yaxisLimit, ref velocity.y,smoothTimeY);
if (halfScreen < 0) {
posY = Mathf.SmoothDamp (transform.position.y, player.transform.position.y, ref velocity.y, smoothTimeY);
} else {
posY = Mathf.SmoothDamp(transform.position.y, transform.position.y, ref velocity.y,smoothTimeY);
}
transform.position = new Vector3(posX,posY,transform.position.z);
}
Now When I jump the camera moves immediately , but I want to move the camera only after the player crosses half height of the camera,How can I do it.
P.s: above code is working but not perfect..Sometimes player goes to very bottom of camera, but camera position is not updated.
Comment