private void Move(Vector2 dir) { if (dir == Vector2.zero) { return; } m_BoxRects = App.manager.obstructMgr.obstructRects; Vector2 absDir = new Vector2(Abs(dir.x), Abs(dir.y)); //使用归一化的方向值 Vector2 speed = dir.normalized; speed.x = absDir.x < 1 ? dir.x : speed.x; speed.y = absDir.y < 1 ? dir.y : speed.y; Vector2 absSpeed = new Vector2(Abs(speed.x), Abs(speed.y)); Vector2 curMove = Vector2.zero; Obstructer.Rect curRect = m_Obstructer.rect; m_PlayerRect = curRect; m_Again = true; while (m_Again && curMove.x <= absDir.x && curMove.y <= absDir.y) { //偏移值 m_PlayerRect.position += speed; Go(speed); curMove += absSpeed; } Vector2 position = m_PlayerRect.position - curRect.position; m_Obstructer.transform.position += new Vector3(position.x, position.y); }
private bool CheckBoxLine(Obstructer.Rect boxRect, Vector2 dir, out Vector2 newDir) { newDir = Vector2.zero; if (!boxRect.Overlaps(m_PlayerRect, out m_OveRect)) { return(false); } //场景碰撞盒的四条边 m_SideLines = m_OveRect.boxLines; #if DRAWDEBUG m_OveRect.DrawBox(Color.blue); #endif float maxValue = m_OveRect.width > m_OveRect.height ? m_OveRect.width : m_OveRect.height; //检测线段 m_DirLine = new Obstructer.line(m_OveRect.center, m_OveRect.center - dir * (maxValue * 2)); for (int index = 0; index < m_SideLines.Length; index++) { if (IsRectCross(m_SideLines[index], m_DirLine)) { //得到相交矩形的非相交轴的长度 if (index < 2) { newDir.y = (dir.y > 0 ? -1 : 1) * (m_OveRect.height + 0.01f); } else { newDir.x = (dir.x > 0 ? -1 : 1) * (m_OveRect.width + 0.01f); } return(true); } } return(false); }