- Home /
Check if part of an object is outside of Camera View
Hi guys!
I want to check in my Scene if a GameObject is partially or completely outsite of my Screen View. With WorldToScreenPoint Im already logging if the transform.position (= middle point) is outside of the camera. The problem is, that I also want to log if some part of the Object is outside. I hope you undestand my problem, if not feel fre to aks :)
Code:
void Update() {
Vector3 playerOnScreenPos = cam.WorldToScreenPoint(gameObject.transform.position);
if (playerOnScreenPos.x < 0 || playerOnScreenPos.x > Screen.width ||
playerOnScreenPos.y < 0 || playerOnScreenPos.y > Screen.height)
Debug.Log("Outside!");
}
Answer by HicorySauce · Dec 26, 2020 at 03:14 AM
@dbrandauer You may want to take a look at the following pages from the API: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnBecameVisible.html https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnBecameInvisible.html Both are methods that are called when the object gets seen / unseen by the camera. In terms of an object being "partially" inside or outside the camera view, this is harder. What I would do is maybe create two or more different objects that are children of the object you care about being fully visible, and count the parent object as fully visible only if all of its children are visible.
Your answer
Follow this Question
Related Questions
Is it Posable to make you'r own Camera Component and not use the standard Unity Camera Component? 1 Answer
Camera not displaying any gameobjects in scene 4 Answers
Object Placement alignment to background image 2 Answers
"Press [e] to open" on stationary object when onTrigger 0 Answers
Free Look Camera 1 Answer