- Home /
How could i make a collision happen based on camera view ?
I'm a beginner and I'm trying to make a game and i need help with collision. How could i make a collision happen based on camera view ,like if the player hits the edge of camera to destroy the player. The camera doesn't move ,I tried adding box collider but if I change game resolution camera gets bigger or smaller based on resolution and the box collider it's not at the edge anymore
Answer by JxWolfe · Jan 02, 2019 at 07:33 PM
What if you raycast from the edges of the screen, and used those points to scale a collider so it fits perfectly inside the camera, no matter what the screen size is, then use OnTriggerExit to destroy the player. -Your collider needs to be a trigger for that
Code:
void Start()
{
RaycastHit hit1;
Ray ray1 = Camera.main.ScreenPointToRay(new Vector3(Screen.width,Screen.height,0));
RaycastHit hit2;
Ray ray2 = Camera.main.ScreenPointToRay(new Vector3(0,0,0));
if (Physics.Raycast(ray1, out hit1)&&Physics.Raycast(ray2, out hit2)){//},100,LayerMask.NameToLayer("Ground"))) {
Debug.Log("Hit1 "+hit1.point.ToString());
Debug.Log("Hit2 "+hit2.point.ToString());
transform.position = new Vector3((hit1.point.x-hit2.point.x)/2+hit2.point.x,(hit1.point.y-hit2.point.y)/2+hit2.point.y,transform.position.z);
transform.localScale = new Vector3(hit1.point.x-hit2.point.x,hit1.point.y-hit2.point.y,transform.localScale.z);
}
else{
Debug.Log("Miss");
}
}
Requirements: (for you)
*must have a box collider that is a trigger
*box collider must be larger than anything the screenSize might cover (MUCH LARGER)
*No mesh renderer needed
*if Camera.main != wanted camera, change Camera.main to a public Camera variable
*Script MUST be on box collider
*Will be effected if under a parent with a SCALE different from (1,1,1)
Enjoy! Tell me if you encounter issues, however, it worked fine on my computer.
yep, but you are going to keep getting the 'Best Answer'
If it helps here is an extension method i use to get the rectangle of an orthographic camera in world space:
public static Rect GetRect (this Camera camera) {
Vector2 cameraExtents = new Vector2 (camera.aspect * camera.orthographicSize, camera.orthographicSize);
return new Rect ((Vector2) camera.transform.position - cameraExtents, cameraExtents * 2);
}
oh, it doesn't contain code to destroy the player, use --- void OnTriggerExit() --- for that
note, it does need to be positioned FLAT on to the camera, otherwise you encounter some issues around the edges of the camera's view... however normally BOTH ortho AND perspective work...
Answer by Snipe76 · Jan 02, 2019 at 07:34 PM
You need to access the renderer component in your gameObject that has a mesh on it. the renderer has a built-in bool that is true or false based on visibility by cameras. Here is an example in one of my scripts:
private void CheckVisibleByCamera()
{
if (Camera.main)
{
if (VisabilityParts[0].GetComponent<Renderer>().isVisible)
{
isVisibleByCamera = true;
}
else
{
isVisibleByCamera = false;
}
}
} //Check if player is visible by the camera.
Note: isVisible will be true if other cameras in the scene see that mesh.
or alternatively you can get the resolution of the camera you are using and convert it to world position and place the box colliders on the edges.
Vector3 pos = Camera.main.WorldToViewportPoint (transform.position);
pos.x = Mathf.Clamp01(pos.x);
pos.y = Mathf.Clamp01(pos.y);
transform.position = Camera.main.ViewportToWorldPoint(pos);