How to change camera position on trigger while camera is being updated each frame by LateUpdate
So I have just completed my first project, the RollABall tutorial from the unity page. However, it seemed kind of boring and I wanted to add some things. One of those things is adding a trigger so that when my ball rolls to a certain area, the camera angle and position would change so that it would more closely follow the ball instead of having a birds-eye view.
After a while, I thought of using a tag and the OnTriggerEnter method that they had us use in the project to detect when the trigger object is touched and then set the camera angle and position to what I wanted. Changing the camera angle works fine but when I try to change the position of the camera, nothing happens. Here is the code for that method
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("PickUp"))
{
other.gameObject.SetActive(false);
CollectCount++;
SetCountsText();
}
if (other.gameObject.CompareTag("CameraChange"))
{
Camera.main.transform.rotation = Quaternion.Euler(20, 0, 0);
Camera.main.transform.position = new Vector3(0, 3, -5);
}
}
After some testing, I think I know why setting the cameras position here doesn't work. In my other script that they had us write for the camera, they put a line of code in the LateUpdate method that updates the cameras position every frame to follow the ball rolling around. if I take that line out, then the camera doesn't follow the ball but does change positions when the trigger is met. Im thinking this might be something to do with the lateupdate calling too quick or something so the transform.position call doesn't end up doing anything. here is the code for that script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public GameObject player;
private Vector3 offset;
// Start is called before the first frame update
void Start()
{
offset = transform.position - player.transform.position;
}
// Update is called once per frame
void LateUpdate()
{
transform.position = player.transform.position + offset;
}
}
does anyone know what I could do here to fix this? Or just a better way, in general, to do what I'm thinking? I couldn't find anything on google after searching for about a half an hour.
(I should also note I'm very new to this, I started like a day and a half ago. I don't know all of C# but I do know java decently well, and they seem pretty similar so far)