- Home /
How do I disable 2D camera follow when my player collides with a trigger object?
New to Unity. New to C#. Not new to programming. I have an idea about what I'm doing wrong, but I haven't found an answer on the forums that works for my particular case.
I need a script that I can add to an invisible object. When my player collides with the object, the script should disable the Camera2DFollow component in the target camera.
Here's the error I'm getting:
The type or namespace name `Camera2DFollow' could not be found. Are you missing a using directive or an assembly reference?
Here's my script:
using UnityEngine;
using System.Collections;
public class ToggleCameraFollow : MonoBehaviour {
public UnityEngine.Camera Camera;
void OnTriggerEnter (Collider other) {
Camera.GetComponent<Camera2DFollow>().enabled = false;
}
void OnTriggerExit (Collider other) {
Camera.GetComponent<Camera2DFollow>().enabled = true;
}
}
hi it seems a na$$anonymous$$g problem.A class named 'Camera' with capital C is aleready declared by unity under UnityEngine.Camera.But the object name u gave is same as the class name 'Camera' with capital C.
Use a different object name like this
public UnityEngine.Camera $$anonymous$$yCamera;
And make sure you drag and drop the camera object to $$anonymous$$yCamera field in the ispector
or If you have only one camera with $$anonymous$$ainCamera Tag,you can use something like this
Camera.main.GetComponent().enabled = BoolValue;
Hope this helps
@sethuraj thanks!
I discovered the problem with using the name Camera
on accident just a few hours ago. Good advise, and the way you explained it makes sense.
I also discovered that I was targeting the wrong script (Camera2DFollow is namespaced under the 2D standard assets), and I was using the wrong collider for my events (was using Box Collider 2D, which fires OnTriggerEnter2D, etc.).
Here's what worked on a sprite object with Box Collider 2D as a trigger:
using UnityEngine;
using System.Collections;
public class ToggleCameraFollow : $$anonymous$$onoBehaviour {
public UnityEngine.Camera camera;
private UnityStandardAssets._2D.Camera2DFollow followCamera;
void Awake () {
followCamera = camera.GetComponent<UnityStandardAssets._2D.Camera2DFollow>();
}
void OnTriggerEnter2D (Collider2D other) {
Debug.Log("Stop Following Player");
followCamera.enabled = false;
}
void OnTriggerExit2D (Collider2D other) {
Debug.Log("Resume Following Player");
followCamera.enabled = true;
}
}
Your answer
Follow this Question
Related Questions
SetActive messes up everything? 1 Answer
How to count from 2nd collision 1 Answer
OnCollisionEnter getting called more than once,OnCollisionEnter called more than once 1 Answer
How can you make an object visible in Unity when you jump on it? 0 Answers
Can someone help me with calling a method from another Game Object? 1 Answer