feat: 支持更新相机高度

This commit is contained in:
richarjiang
2025-09-22 17:45:20 +08:00
parent 98742745eb
commit 0b270ff9f9
3 changed files with 165 additions and 263 deletions

View File

@@ -52,7 +52,7 @@
}, },
"autoReleaseAssets": false, "autoReleaseAssets": false,
"_globals": { "_globals": {
"__id__": 46 "__id__": 43
}, },
"_id": "58132e64-0171-4c7f-89be-a2984ca7de6b" "_id": "58132e64-0171-4c7f-89be-a2984ca7de6b"
}, },
@@ -80,9 +80,6 @@
{ {
"__id__": 31 "__id__": 31
}, },
{
"__id__": 34
},
{ {
"__id__": 6 "__id__": 6
} }
@@ -90,13 +87,13 @@
"_active": true, "_active": true,
"_components": [ "_components": [
{ {
"__id__": 43 "__id__": 40
}, },
{ {
"__id__": 44 "__id__": 41
}, },
{ {
"__id__": 45 "__id__": 42
} }
], ],
"_prefab": null, "_prefab": null,
@@ -282,7 +279,7 @@
"_prefab": null, "_prefab": null,
"_lpos": { "_lpos": {
"__type__": "cc.Vec3", "__type__": "cc.Vec3",
"x": 122.796, "x": 123.77,
"y": -26.144, "y": -26.144,
"z": 0 "z": 0
}, },
@@ -587,13 +584,13 @@
"_restitution": 0, "_restitution": 0,
"_offset": { "_offset": {
"__type__": "cc.Vec2", "__type__": "cc.Vec2",
"x": -2.9, "x": 0,
"y": -7.3 "y": 0
}, },
"_size": { "_size": {
"__type__": "cc.Size", "__type__": "cc.Size",
"width": 31.5, "width": 39.3,
"height": 70.3 "height": 55.7
}, },
"_id": "634GiEUhBB8Z1aGzkT6Zyd" "_id": "634GiEUhBB8Z1aGzkT6Zyd"
}, },
@@ -915,14 +912,14 @@
}, },
{ {
"__type__": "cc.Node", "__type__": "cc.Node",
"_name": "Bg", "_name": "PlayerController",
"_objFlags": 0, "_objFlags": 0,
"__editorExtras__": {}, "__editorExtras__": {},
"_parent": { "_parent": {
"__id__": 2 "__id__": 2
}, },
"_children": [], "_children": [],
"_active": false, "_active": true,
"_components": [ "_components": [
{ {
"__id__": 26 "__id__": 26
@@ -959,7 +956,7 @@
"y": 0, "y": 0,
"z": 0 "z": 0
}, },
"_id": "9fWIW0nlZOAKYYrvMjuWpy" "_id": "2dBJLKIX9BV4ynG/qUL0Kd"
}, },
{ {
"__type__": "cc.UITransform", "__type__": "cc.UITransform",
@@ -973,18 +970,18 @@
"__prefab": null, "__prefab": null,
"_contentSize": { "_contentSize": {
"__type__": "cc.Size", "__type__": "cc.Size",
"width": 1080, "width": 100,
"height": 2560 "height": 100
}, },
"_anchorPoint": { "_anchorPoint": {
"__type__": "cc.Vec2", "__type__": "cc.Vec2",
"x": 0.5, "x": 0.5,
"y": 0.5 "y": 0.5
}, },
"_id": "45vczKBs5CfZpW98yME17u" "_id": "6bjIx8+UNMz47XvRycFFNU"
}, },
{ {
"__type__": "cc.Sprite", "__type__": "a1b2cPU5fZ4kKvN7xI0VniQ",
"_name": "", "_name": "",
"_objFlags": 0, "_objFlags": 0,
"__editorExtras__": {}, "__editorExtras__": {},
@@ -993,38 +990,23 @@
}, },
"_enabled": true, "_enabled": true,
"__prefab": null, "__prefab": null,
"_customMaterial": null, "player": {
"_srcBlendFactor": 2, "__id__": 6
"_dstBlendFactor": 4,
"_color": {
"__type__": "cc.Color",
"r": 255,
"g": 255,
"b": 255,
"a": 255
}, },
"_spriteFrame": { "camera": {
"__uuid__": "361c5873-d797-420e-be65-81c5a8f91215@f9941", "__id__": 4
"__expectedType__": "cc.SpriteFrame"
}, },
"_type": 0, "pathfinder": {
"_fillType": 0, "__id__": 24
"_sizeMode": 1,
"_fillCenter": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
}, },
"_fillStart": 0, "moveSpeed": 300,
"_fillRange": 0, "mapWidth": 1080,
"_isTrimmedMode": true, "mapHeight": 2560,
"_useGrayscale": false, "_id": "c1AuAU3IlKnLOzgk9vsBr4"
"_atlas": null,
"_id": "f9zWrrB85EzKBNoudMPMro"
}, },
{ {
"__type__": "cc.Node", "__type__": "cc.Node",
"_name": "PlayerController", "_name": "Manager",
"_objFlags": 0, "_objFlags": 0,
"__editorExtras__": {}, "__editorExtras__": {},
"_parent": { "_parent": {
@@ -1068,100 +1050,6 @@
"y": 0, "y": 0,
"z": 0 "z": 0
}, },
"_id": "2dBJLKIX9BV4ynG/qUL0Kd"
},
{
"__type__": "cc.UITransform",
"_name": "",
"_objFlags": 0,
"__editorExtras__": {},
"node": {
"__id__": 28
},
"_enabled": true,
"__prefab": null,
"_contentSize": {
"__type__": "cc.Size",
"width": 100,
"height": 100
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
},
"_id": "6bjIx8+UNMz47XvRycFFNU"
},
{
"__type__": "a1b2cPU5fZ4kKvN7xI0VniQ",
"_name": "",
"_objFlags": 0,
"__editorExtras__": {},
"node": {
"__id__": 28
},
"_enabled": true,
"__prefab": null,
"player": {
"__id__": 6
},
"camera": {
"__id__": 4
},
"pathfinder": {
"__id__": 24
},
"moveSpeed": 300,
"mapWidth": 1080,
"mapHeight": 2560,
"_id": "c1AuAU3IlKnLOzgk9vsBr4"
},
{
"__type__": "cc.Node",
"_name": "Manager",
"_objFlags": 0,
"__editorExtras__": {},
"_parent": {
"__id__": 2
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 32
},
{
"__id__": 33
}
],
"_prefab": null,
"_lpos": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_lrot": {
"__type__": "cc.Quat",
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"_lscale": {
"__type__": "cc.Vec3",
"x": 1,
"y": 1,
"z": 1
},
"_mobility": 0,
"_layer": 33554432,
"_euler": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_id": "faEV0Z9e5HPJZltMJt2Zn9" "_id": "faEV0Z9e5HPJZltMJt2Zn9"
}, },
{ {
@@ -1170,7 +1058,7 @@
"_objFlags": 0, "_objFlags": 0,
"__editorExtras__": {}, "__editorExtras__": {},
"node": { "node": {
"__id__": 31 "__id__": 28
}, },
"_enabled": true, "_enabled": true,
"__prefab": null, "__prefab": null,
@@ -1192,7 +1080,7 @@
"_objFlags": 0, "_objFlags": 0,
"__editorExtras__": {}, "__editorExtras__": {},
"node": { "node": {
"__id__": 31 "__id__": 28
}, },
"_enabled": true, "_enabled": true,
"__prefab": null, "__prefab": null,
@@ -1214,25 +1102,25 @@
}, },
"_children": [ "_children": [
{ {
"__id__": 35 "__id__": 32
} }
], ],
"_active": true, "_active": true,
"_components": [ "_components": [
{
"__id__": 35
},
{
"__id__": 36
},
{
"__id__": 37
},
{ {
"__id__": 38 "__id__": 38
}, },
{ {
"__id__": 39 "__id__": 39
},
{
"__id__": 40
},
{
"__id__": 41
},
{
"__id__": 42
} }
], ],
"_prefab": null, "_prefab": null,
@@ -1271,16 +1159,16 @@
"_objFlags": 0, "_objFlags": 0,
"__editorExtras__": {}, "__editorExtras__": {},
"_parent": { "_parent": {
"__id__": 34 "__id__": 31
}, },
"_children": [], "_children": [],
"_active": true, "_active": true,
"_components": [ "_components": [
{ {
"__id__": 36 "__id__": 33
}, },
{ {
"__id__": 37 "__id__": 34
} }
], ],
"_prefab": null, "_prefab": null,
@@ -1319,7 +1207,7 @@
"_objFlags": 0, "_objFlags": 0,
"__editorExtras__": {}, "__editorExtras__": {},
"node": { "node": {
"__id__": 35 "__id__": 32
}, },
"_enabled": true, "_enabled": true,
"__prefab": null, "__prefab": null,
@@ -1341,7 +1229,7 @@
"_objFlags": 0, "_objFlags": 0,
"__editorExtras__": {}, "__editorExtras__": {},
"node": { "node": {
"__id__": 35 "__id__": 32
}, },
"_enabled": true, "_enabled": true,
"__prefab": null, "__prefab": null,
@@ -1403,7 +1291,7 @@
"_objFlags": 0, "_objFlags": 0,
"__editorExtras__": {}, "__editorExtras__": {},
"node": { "node": {
"__id__": 34 "__id__": 31
}, },
"_enabled": true, "_enabled": true,
"__prefab": null, "__prefab": null,
@@ -1425,7 +1313,7 @@
"_objFlags": 0, "_objFlags": 0,
"__editorExtras__": {}, "__editorExtras__": {},
"node": { "node": {
"__id__": 34 "__id__": 31
}, },
"_enabled": true, "_enabled": true,
"__prefab": null, "__prefab": null,
@@ -1464,7 +1352,7 @@
"_objFlags": 0, "_objFlags": 0,
"__editorExtras__": {}, "__editorExtras__": {},
"node": { "node": {
"__id__": 34 "__id__": 31
}, },
"_enabled": true, "_enabled": true,
"__prefab": null, "__prefab": null,
@@ -1495,7 +1383,7 @@
"_objFlags": 0, "_objFlags": 0,
"__editorExtras__": {}, "__editorExtras__": {},
"node": { "node": {
"__id__": 34 "__id__": 31
}, },
"_enabled": true, "_enabled": true,
"__prefab": null, "__prefab": null,
@@ -1523,7 +1411,7 @@
"_objFlags": 0, "_objFlags": 0,
"__editorExtras__": {}, "__editorExtras__": {},
"node": { "node": {
"__id__": 34 "__id__": 31
}, },
"_enabled": true, "_enabled": true,
"__prefab": null, "__prefab": null,
@@ -1616,28 +1504,28 @@
{ {
"__type__": "cc.SceneGlobals", "__type__": "cc.SceneGlobals",
"ambient": { "ambient": {
"__id__": 47 "__id__": 44
}, },
"shadows": { "shadows": {
"__id__": 48 "__id__": 45
}, },
"_skybox": { "_skybox": {
"__id__": 49 "__id__": 46
}, },
"fog": { "fog": {
"__id__": 50 "__id__": 47
}, },
"octree": { "octree": {
"__id__": 51 "__id__": 48
}, },
"skin": { "skin": {
"__id__": 52 "__id__": 49
}, },
"lightProbeInfo": { "lightProbeInfo": {
"__id__": 53 "__id__": 50
}, },
"postSettings": { "postSettings": {
"__id__": 54 "__id__": 51
}, },
"bakedWithStationaryMainLight": false, "bakedWithStationaryMainLight": false,
"bakedWithHighpLightmap": false "bakedWithHighpLightmap": false

View File

@@ -27,6 +27,8 @@ export class CameraFollow extends Component {
onLoad() { onLoad() {
// 获取相机组件 // 获取相机组件
this.camera = this.getComponent(Camera); this.camera = this.getComponent(Camera);
this.camera.orthoHeight = 500
if (!this.camera) { if (!this.camera) {
console.error('CameraFollow: 未找到Camera组件'); console.error('CameraFollow: 未找到Camera组件');
} }

View File

@@ -85,12 +85,24 @@ export class PlayerController extends Component {
const normalizedX = screenPos.x - centerX; const normalizedX = screenPos.x - centerX;
const normalizedY = screenPos.y - centerY; const normalizedY = screenPos.y - centerY;
// 获取相机的正交高度
const orthoHeight = this.camera.orthoHeight;
// 计算屏幕坐标到世界坐标的缩放比例
// 屏幕高度的一半对应 orthoHeight 的世界单位
const scaleY = orthoHeight / (visibleSize.height * 0.5);
const scaleX = scaleY; // 保持宽高比一致
// 将屏幕坐标转换为世界坐标(相对于相机中心)
const worldOffsetX = normalizedX * scaleX;
const worldOffsetY = normalizedY * scaleY;
// 考虑相机的位置偏移 // 考虑相机的位置偏移
const cameraPos = this.camera.node.position; const cameraPos = this.camera.node.position;
// 计算世界坐标 // 计算最终的世界坐标
const worldX = normalizedX + cameraPos.x; const worldX = worldOffsetX + cameraPos.x;
const worldY = normalizedY + cameraPos.y; const worldY = worldOffsetY + cameraPos.y;
return new Vec3(worldX, worldY, 0); return new Vec3(worldX, worldY, 0);
} }