本文共 2092 字,大约阅读时间需要 6 分钟。
private static GameObject s_aroundCols; public static void CreateAroundColliders() { if(s_aroundCols != null) return; s_aroundCols = new GameObject("aroundCols"); s_aroundCols.transform.position = Vector3.zero; s_aroundCols.transform.localScale = Vector3.one; s_aroundCols.transform.localEulerAngles = Vector3.zero; Vector3 top = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width / 2f, Screen.height, 3f)); Vector3 bottom = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width / 2f, 0f, 3f)); Vector3 left = Camera.main.ScreenToWorldPoint(new Vector3(0f, Screen.height/2f, 3f)); Vector3 right = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height/2f, 3f)); float width = Vector3.Distance(left, right); float height = Vector3.Distance(bottom, top); PhysicMaterial pmat = new PhysicMaterial(); pmat.dynamicFriction = 0f; //动摩擦力// pmat.staticFriction = 0f; //静摩擦力// pmat.bounciness = 1f; //弹力// pmat.frictionCombine = PhysicMaterialCombine.Maximum; //接触物体之间的摩擦力计算// pmat.bounceCombine = PhysicMaterialCombine.Maximum; //接触物体之间的弹力计算// createOnCollider("top", s_aroundCols.transform, Vector3.zero, top, new Vector3(width, 0.05f, 0.05f), pmat); createOnCollider("bottom", s_aroundCols.transform, Vector3.zero, bottom, new Vector3(width, 0.05f, 0.05f), pmat); createOnCollider("left", s_aroundCols.transform, Vector3.zero, left, new Vector3(0.05f, height, 0.05f), pmat); createOnCollider("right", s_aroundCols.transform, Vector3.zero, right, new Vector3(0.05f, height, 0.05f), pmat); } private static void createOnCollider(string name, Transform parent, Vector3 pos, Vector3 center, Vector3 size, PhysicMaterial mat) { GameObject col = new GameObject(name); col.transform.position = pos; col.transform.parent = parent; BoxCollider box = col.AddComponent(); box.center = center; box.size = size; box.material = mat; }
如上所示
转载地址:http://ogzvf.baihongyu.com/