The main camera currently follows the player whenever he moves, that’s fine but I want the camera to stop following the player enters a game object that I placed on the scene. And yes I did attach the script to the game object.
Here’s the script attached to the Main Camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerFollow : MonoBehaviour
{
public Transform PlayerTransform;
private Vector3 _cameraOffset;
[Range(0.01f, 1.0f) ]
public float SmoothFactor = 0.5f;
// Start is called before the first frame update
void Start()
{
_cameraOffset = transform.position - PlayerTransform.position;
}
// LateUpdate is called after Update methods
void LateUpdate()
{
Vector3 newPos = PlayerTransform.position + _cameraOffset;
transform.position = Vector3.Slerp(transform.position, newPos, SmoothFactor);
}
}
And here’s the script that I want to disable the camera following you:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StopCamera : MonoBehaviour
{
Camera m_MainCamera; //main camera in the scene
// Start is called before the first frame update
void Start()
{
m_MainCamera = GetComponent<Camera>();
}
void OnCollisionEnter(Collision2D col)
{
if (col.gameObject.tag == "Player")
m_MainCamera.enabled = false;
{
}
}
}
You’ve got extra curly braces hanging out in space on lines 23/24 of your second script. Line 22 is just going to shut off the camera, not do anything about the PlayerFollow script.
You haven’t mentioned what problem you are having though. Those are just the most obvious issues to me. Also, I’m not clear what object your second script is attached to. You imply it is attached to some object the player will run into, but on like 14 you are getting a reference to the Camera component attached to the same object. Which means instead you are attaching this script to the camera object, not this other object. Confusing what you’re doing here.