- Home /
Making camera follow upwards
How do I make the camera follow my character upwards at a certain height, I want the horizontal camera to move upwards after I reach a bit under the top like in New Super Mario Bros. I also need the camera to go back down after the character get under the red line in the photo. I am using unity 5.6.6
I want the camera to follow after the character reaches the red line
So this is my current code, I;m only missing what I mentioned
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour {
public GameObject target;
public float followAhead;
private Vector3 targetPosition;
public float smoothing;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update() {
targetPosition = new Vector3(target.transform.position.x, transform.position.y, transform.position.z);
//Makes camera move ahead of player
if (target.transform.localScale.x > 0f)
{
targetPosition = new Vector3(targetPosition.x + followAhead, targetPosition.y, targetPosition.z);
} else {
targetPosition = new Vector3(targetPosition.x - followAhead, targetPosition.y, targetPosition.z);
}
//transform.position = targetPosition;
transform.position = Vector3.Lerp(transform.position, targetPosition, smoothing * Time.deltaTime);
}
}
Answer by Atiyeh123 · Sep 16, 2018 at 05:31 PM
hi, if you want camera follows player constantly you can see this link: https://unity3d.com/learn/tutorials/projects/2d-ufo-tutorial/following-player-camera but in if you want to follow the player only up the red line you have to store the first y position and use that after player moves down. I'v changed your code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMovement : MonoBehaviour
{
public float redLineY;
public float firstCameraY;
public GameObject target;
public float followAhead;
private Vector3 targetPosition;
public float smoothing;
private Vector3 targetOffset = new Vector3(0,0,0);
// Update is called once per frame
void Update()
{
//Makes camera move ahead of player
if (target.transform.position.y > redLineY)
{
targetPosition = new Vector3(target.transform.position.x, target.transform.position.y, transform.position.z) + targetOffset;//you can change the offset
}
else
{
targetPosition = new Vector3(target.transform.position.x, firstCameraY, transform.position.z);
}
//transform.position = targetPosition;
transform.position = Vector3.Lerp(transform.position, targetPosition, smoothing * Time.deltaTime);
}
}
Answer by unity_KPzA8Ravb2HEzQ · Sep 16, 2018 at 11:04 PM
Thank you so much that works it's following up the way I want beautifully but it doesn't follow ahead in front of the character so I added something to the code but it won't follow ahead the other way when I turn around. Sorry I'm kinda new, also the camera is perfect for my game, I love it so don't worry too much about helping me fix it again :D
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public float redLineY;
public float firstCameraY;
public GameObject target;
public float followAhead;
private Vector3 targetPosition;
public float smoothing;
private Vector3 targetOffset = new Vector3(0,0,0);
// Update is called once per frame
void Update()
{
//Makes camera move ahead of player
if (target.transform.position.y > redLineY)
{
targetPosition = new Vector3(target.transform.position.x + followAhead, target.transform.position.y, transform.position.z) + targetOffset;//you can change the offset
}
else
{
targetPosition = new Vector3(target.transform.position.x - followAhead, firstCameraY, transform.position.z);
}
//transform.position = targetPosition;
transform.position = Vector3.Lerp(transform.position, targetPosition, smoothing * Time.deltaTime);
}
}
Answer by Ciphon_Gaming · Jul 19, 2021 at 07:41 AM
how do you make it is that it only follows it upwards?
Your answer
Follow this Question
Related Questions
Smooth camera on moving platform 1 Answer
Help make camera zoom smoother 3 Answers
Problems making a smash bros like camera 2 Answers
How to make a camera Follow an Object moving in zigzag path? 1 Answer
Camera move & rotate along vehicle 0 Answers