- Home /
Smooth Camera follow for 2D platformer game
Hey Guys,
I am working on a clasical 2D platformer game somethig similar to Mario Bros. The game is working fine. Camera follows the player smoothly. But when I jump the camera still follows the player. What I want is the camera to wait until the player is above its 1/2. I want the camera to follow the player on the X axis smoothly and constantly - that is working fine. But on the Y axis I want the camera to wait and as soon as the player touches more than 1/2 of the camera range then change the transform of the camera on the Y axis exactly like Mario Bros.
Here is my simple smooth follow script that follows the player all the time:
public float dampTime = 0.25f;
private Vector3 velocity = Vector3.zero;
public Transform target;
void Update ()
{
if (target.position.x > 0)
{
Vector3 point = camera.WorldToViewportPoint(target.position);
Vector3 delta = target.position - camera.ViewportToWorldPoint(new Vector3(0.3f, 0.4f, point.z));
Vector3 destination = transform.position + delta;
transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
}
}
Answer by chariot · Feb 10, 2015 at 08:30 AM
Use for smooth cam Mathf.Lerp
public var cameraObj : GameObject;
public var specificVector : Vector3;
public var smoothSpeed : float;
function Start () {
cameraObj = GameObject.FindGameObjectWithTag("MainCam");
}
function Update () {
specificVector = Vector3(transform.position.x, transform.position.y, cameraObj.transform.position.z);
cameraObj.transform.position = Vector3.Lerp(cameraObj.transform.position, specificVector, smooth * Time.deltaTime);
}
Thanks for the answer but the script is not working. I am not good in JS. I changed the tag to "$$anonymous$$ainCamera", set the smoothSpeed and changed smooth Time.deltaTime to "... smoothSpeed Time.deltaTime". Does this script know what to track? Where is the section that the camera has to wait until the player is higher then 1/2 of the rendering view?
Can anyone help me please with this in C#?
Im give you reference - link
C# can be found there.
$$anonymous$$y script track character without any expressions, u cant do it urself ("if" constructions)
Ok, I got it working. But the camera still folows the player smoothly. I want the camera to wait when the player jumps (so on the Y axes) until he reaches some upper point of the rendering area and afterwards smoothly tracks the player. Is it clear? $$anonymous$$y english is not so good :-/
Why? Just use global position. In editor turn your character at height.
At first image orc centered to cam view (transform.position.y = -1), at second he upside cam view (transform.position.y = 1). So default to cam view - cameraObj.transform.position.y = transform.position.y -1; So what we must to do next:
public var cameraObj : GameObject;
public var specificVector : Vector3;
public var smoothSpeed : float;
function Start () {
//just turn camera in Inspector, for example
}
function Update () {
if(cameraObj.transform.position.y < transform.position.y - 2){
specificVector = Vector3(transform.position.x, transform.position.y - 2, cameraObj.transform.position.z);
cameraObj.transform.position = Vector3.Lerp(cameraObj.transform.position, specificVector, smoothSpeed * Time.deltaTime);
}
else if (cameraObj.transform.position.y > transform.position.y + 2){
specificVector = Vector3(transform.position.x, transform.position.y + 2, cameraObj.transform.position.z);
cameraObj.transform.position = Vector3.Lerp(cameraObj.transform.position, specificVector, smoothSpeed * Time.deltaTime);
}
else {
specificVector = Vector3(transform.position.x, cameraObj.transform.position.y, cameraObj.transform.position.z);
cameraObj.transform.position = Vector3.Lerp(cameraObj.transform.position, specificVector, smoothSpeed * Time.deltaTime);
}
}
edited
Your answer
![](https://koobas.hobune.stream/wayback/20220613182906im_/https://answers.unity.com/themes/thub/images/avi.jpg)