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
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.
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;
}
}
}