- Home /
camera script not working
Hi there, I’m making a 2d game, is almost finished but the camera scripts is missing, I’d like to make a camera like super mario, I created a function to follow the player it work, but when the transformation changes the camera stops following him. helpe me please =) !
public class Cam : MonoBehaviour
{
public Transform player;
// public Transform [] target; //you can make an array of transform???
/// 1 transformation
/// 2 transformation
/// 3 transformation
public float distanceToPlayer;
public float speedCam;
public float height;
private void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
}
void Follow()
{
Vector3 newPosition = player.position + new Vector3(0, height, -distanceToPlayer);
transform.position = Vector3.Lerp(transform.position, newPosition, speedCam * Time.deltaTime);
//
}
private void Update()
{
Follow();
}
}
What do you mean by "when the transformation" changes?
Is your player able to control several gameObjects during the game? And when the gameObject the player can control changes, you want the camera to start follow the new gameObject, right?
You know super mario bros? the mechanics is the same, i have 3 prefabs (transformation)
that are already present on scene, they are activated and deactivated with (SetActive) based on what they collect. the problem occurs when the pefab changes! I want the camera to always follow the object even if it is replaced with another. thaks a lot!
the Script "PlayerController" is attached to all three prefabs.
Answer by smillyfaces · Jul 29, 2020 at 03:00 PM
2 things first u can attach the camera to the player and it will follow without a script, second if ur transform is public u can put the player in the slot in the editor elimating the need for the start method, ill try to find some other solutions if these dont work
don't work... Is possible to create a transform array based on active transformation? but I have no idea how to write..