|
Post by bacall on Jan 14, 2018 22:35:09 GMT
Hi!
How would you guys go about creating collision between Collider2Ds and the Camera Boundaries? I would want to turn the "camera window" into Edge Colliders. Have anyone done this already? All suggestions are appreciated!
I would want to do this for example: when you can't backtrack (like in Super Mario). I would also need it for keeping Rigidbodies from bouncing out of the camera window (once inside the frames).
I know this can be done by setting up an Edge Collider around the camera window, but then every player has a different screen size, and resolution, and sometimes the Pro Camera 2D changes boundaries (when entering a room e.t.c), and a lot of other troubles occur. It would be so much easier with an auto resizing Edge Collider, which matches the Camera Window.
How would this be done? I already know we can create our own Extensions, but I need something more concrete.
Thank you!
|
|
|
Post by Luís Pedro Fonseca on Jan 15, 2018 10:29:54 GMT
bacallWhat issues are you encountering when implementing this? This is something that you don't even need ProCamera2D to do. You can get the boundaries from any Unity camera and from there you can update the edge colliders. Have you tried looking for a general solution instead of something specific to ProCamera2D?
|
|
|
Post by bacall on Jan 15, 2018 21:30:27 GMT
I figured it would be easier or more convenient regarding the Extensions. The Pro Camera 2D already has some type of rays/variables/checking/info I could make use of instead of reinventing. I see how the Gizmos change in the editor (for example drawing the camera window), can I make use of these somehow? Otherwise I'd turn to some general solution,
Edit: Any pointing me in the right direction is a lot appreciated, since you are the camera master
|
|
|
Post by Luís Pedro Fonseca on Jan 17, 2018 16:12:01 GMT
bacall You can use ProCamera2D to get the screen size in world coordinates (there's a method with a similar name). Then all you need is from the center of the camera, to figure out the 4 corners. Create a EdgeCollider2D for each side (top, bottom, left, right). That should be it. Let me know if you encounter some trouble.
|
|
|
Post by bacall on Jan 18, 2018 0:52:34 GMT
I'm posting this if someone else needs it... This is what I came up with, it's probably not the best way, or even correct, but it sure seems to work perfectly fine for me. I know it's not the best to put "UpdateEdgeCollider();" on every Update. Maybe I could put it under a "if (screenSize != previousScreenSize)", to only update when the screenSize changes. I put my EdgeCollider on my camera, together with a Kinematic Rigidbody2D (because that's what Unity tells us to do).
And also, please let me know if anyone has a better way, I am open for all suggestions. Thanks!
using System.Collections.Generic; using UnityEngine; using Com.LuisPedroFonseca.ProCamera2D;
public class ProCamera2DEdgeCollider : MonoBehaviour {
public List<Vector2> newVerticies = new List<Vector2>(); private EdgeCollider2D col;
public float extrude = 1f; //default value is 1 - used if you want to extrude or intrude edges
Vector2 screenSize;
// Use this for initialization void Start () { col = GetComponent<EdgeCollider2D>(); } // Update is called once per frame void Update () { UpdateEdgeCollider(); }
void UpdateEdgeCollider() { screenSize = ProCamera2D.Instance.ScreenSizeInWorldCoordinates; //get screen size
newVerticies.Clear(); //clear previous points
newVerticies.Add(new Vector2(-screenSize.x, -screenSize.y) * 0.5f * extrude); //0.5 cuz we only need half that value for distance away from the camera to the edge newVerticies.Add(new Vector2(screenSize.x, -screenSize.y) * 0.5f * extrude); newVerticies.Add(new Vector2(screenSize.x, screenSize.y) * 0.5f * extrude); newVerticies.Add(new Vector2(-screenSize.x, screenSize.y) * 0.5f * extrude); newVerticies.Add(new Vector2(-screenSize.x, -screenSize.y) * 0.5f * extrude);
//Super mario style with left wall collision, extended so player doesn't jump over, remove above "newVerticies" //newVerticies.Add(new Vector2(-screenSize.x, screenSize.y * extrude) * 0.5f); //we can increase the extrude value to make the wall higher //newVerticies.Add(new Vector2(-screenSize.x, -screenSize.y * extrude) * 0.5f);
col.points = newVerticies.ToArray(); } }
|
|
|
Post by Luís Pedro Fonseca on Jan 18, 2018 9:30:35 GMT
This looks good to me. Like you said, the only thing you could improve is not having that updating unless is necessary. For that, you can use the "OnCameraResize" event from ProCamera2D and only update when needed. Any how, great work and thanks for sharing!
|
|
|
Post by bacall on Jan 18, 2018 22:08:54 GMT
Did you mean like this? And then I can grab the screenSize from the Vector2 in the OnCameraResize event? Thanks for the help!
public List<Vector2> newVerticies = new List<Vector2>(); private EdgeCollider2D col;
public float extrude = 1f; //default value is 1
// Use this for initialization void Start () { col = GetComponent<EdgeCollider2D>(); ProCamera2D.Instance.OnCameraResize += OnCameraResizeEvent;
//Otherwise it won't update until the first screen size change Vector2 screenSize = ProCamera2D.Instance.ScreenSizeInWorldCoordinates; //get screen size OnCameraResizeEvent(screenSize); } void OnCameraResizeEvent(Vector2 s) { newVerticies.Clear(); //clear previous points
newVerticies.Add(new Vector2(-s.x, -s.y) * 0.5f * extrude); //0.5 cuz we only need half that value for distance away from the camera to the edge newVerticies.Add(new Vector2(s.x, -s.y) * 0.5f * extrude); newVerticies.Add(new Vector2(s.x, s.y) * 0.5f * extrude); newVerticies.Add(new Vector2(-s.x, s.y) * 0.5f * extrude); newVerticies.Add(new Vector2(-s.x, -s.y) * 0.5f * extrude);
col.points = newVerticies.ToArray(); }
|
|
|
Post by Luís Pedro Fonseca on Jan 19, 2018 10:48:21 GMT
That looks perfect!
|
|