Learning Unity: Problem with camera follow objet...

Hi guys.

I´m learning Unity, and now, I´m in camera lesson but have a praoblem:

I want that main camera, follow “furgoneta” objet but x,z,y axes go crazy!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Camarajugador : MonoBehaviour
{
    public GameObject Camfurgoneta;

    // Start is called before the first frame update
    void Start()
    {
     
    }

    // Update is called once per frame
    void Update()
    {
         transform.position = Camfurgoneta.transform.position + new Vector3(4,3,-19);
    }
}

When start simulation, z axes doen´t corarect value

What can I do?

before to star simulation

in-game start simulation

You see that x axe scrolls to right

Hey, this is one of the challenges of Create with Code, isn’t it? :wink:

There is a simpler way to do a camera follow: Moving the Camera - Unity Learn

It’s one of the first scripts we learn, I encourage you to do the Roll a Ball tutorial, you’ll learn a lot. :wink:

Ok, thanks for your quickly question :slight_smile:

My scrip only work if a objet (player) start in 0,0,0 values,

I´m read this tutorial, Thx so much

Here is the script I wrote for that challenge. It uses the new input manager and the camera switches between three different positions. You can study it and maybe people around will give advice. :wink:

FollowPlayer.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class FollowPlayer : MonoBehaviour
{
    [SerializeField] private GameObject vehicle;
    [SerializeField] private GameObject[] cameraPositions;

    // The distance between the camera and the vehicle
    private Vector3 offset;

    private bool camera1, camera2, camera3;


    #region Input System begins here

    [SerializeField] private InputActionAsset globalInputsActionAsset;

    private InputActionMap cameraSwitcherActionMap;
    private InputAction switchCameraInputAction;

    private void Awake()
    {
        // Camera switching
        cameraSwitcherActionMap = globalInputsActionAsset.FindActionMap("CameraSwitcher");
        switchCameraInputAction = cameraSwitcherActionMap.FindAction("SwitchCamera");
        switchCameraInputAction.performed += context => SwitchCamera();
    }

    private void OnEnable()
    {
        switchCameraInputAction.Enable();
    }

    private void OnDisable()
    {
        switchCameraInputAction.Disable();
    }

    #endregion Input System ends here


    // Start is called before the first frame update
    private void Start()
    {
        // Placing the camera at is default position
        transform.position = cameraPositions[0].transform.position;
        // Setting this bool true to help with the camera switching code
        camera1 = true;
        // Calculating the distance between the camera and the vehicle
        offset = transform.position - vehicle.transform.position;
    }

    private void SwitchCamera()
    {
        SwitchCameraPosition();
    }

    private void SwitchCameraPosition()
    {
        if (camera1)
        {
            // Positionning camera at the second position
            transform.position = cameraPositions[1].transform.position;
            transform.Rotate(new Vector3(0, -90, 15));
            offset = transform.position - vehicle.transform.position;
            camera1 = false;
            camera2 = true;
        }
        else if (camera2)
        {
            // Positionning camera at the third position
            transform.position = cameraPositions[2].transform.position;
            transform.Rotate(new Vector3(0, 90, 0));
            offset = transform.position - vehicle.transform.position;
            camera2 = false;
            camera3 = true;
        }
        else if (camera3)
        {
            // Positionning camera at the default position
            transform.position = cameraPositions[0].transform.position;
            transform.Rotate(new Vector3(15, 0, 0));
            offset = transform.position - vehicle.transform.position;
            camera3 = false;
            camera1 = true;
        }
    }

    // Update is called once per frame
    private void LateUpdate()
    {
        // Applying the distance between the camera and the vehicle
        transform.position = vehicle.transform.position + offset;

        // Synchronizing vehicle and camera rotation
        if (camera3)
        {
            transform.rotation = vehicle.transform.rotation;
        }
    }
}

Thanks guys I learm very much with you :slight_smile: