直接貼上代碼,旋轉角色角度檢測碰撞spa
Vector2 v2Normal = new Vector2(normal.x, normal.y); float fAngle = Vector2.SignedAngle(v2Normal, Vector2.up); gameObject.transform.localRotation = Quaternion.Euler(0, fAngle,0); Vector3 startPos = new Vector3(transform.position.x, transform.position.y+2, transform.position.z); Vector3 v3Dir =transform.TransformDirection(Vector3.forward); Vector3 endPos = startPos + v3Dir * Time.deltaTime * mMoveSpeed; // 檢測是否碰到模型 RaycastHit hitInfo; if (Physics.Linecast(startPos, endPos, out hitInfo, 1 << LayerMask.NameToLayer("Wall"))) { // 貼着模型行走 for (int i = 0; i < 72; i++) { fAngle += 5.0f; gameObject.transform.localRotation = Quaternion.Euler(0, fAngle, 0); v3Dir = transform.TransformDirection(Vector3.forward); endPos = startPos + v3Dir * Time.deltaTime * mMoveSpeed; if (!Physics.Linecast(startPos, endPos, out hitInfo, 1 << LayerMask.NameToLayer("Wall"))) { gameObject.transform.Translate(Vector3.forward * Time.deltaTime * mMoveSpeed); break; } } } else { gameObject.transform.Translate(Vector3.forward * Time.deltaTime * mMoveSpeed); }