我做了一个小地图,我对相机的感到困惑。FieldOfView:玩家在小地图上的位置是正确的,但玩家(相机)的视点看到另一个依赖于 FieldOfView 的点。
小地图类:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class MiniMap : MonoBehaviour
{
public Transform Target;
public Image meImage;
public RawImage minimap;
public GameObject rotatingCube;
public float Scale = 10;
Vector2 XRotation = Vector2.right;
Vector2 YRotation = Vector2.up;
private RectTransform rectTransform;
private void Start()
{
rectTransform = GetComponent<RectTransform>();
}
private void LateUpdate()
{
XRotation = new Vector2(Target.right.x, - Target.right.z);
YRotation = new Vector2(-Target.forward.x, Target.forward.z);
}
public Vector2 TransformPosition(Vector3 position)
{
Vector3 offset = position - Target.position;
Vector2 newPosition = new Vector2(-offset.x, -offset.z);
return newPosition;
}
public Vector3 TransformRotation(Vector3 rotation)
{
return new Vector3(0, 0 , Target.eulerAngles.y - rotation.y);
}
public Vector2 MoveInside(Vector2 point, Rect rect)
{
Rect mapRect = GetComponent<RectTransform>().rect;
point = Vector2.Max(point, mapRect.min);
point = Vector2.Min(point, mapRect.max);
point.x = Mathf.Min(point.x, mapRect.width / 2 - rect.width);
point.y = Mathf.Min(point.y, mapRect.height / 2 - rect.height);
return point;
}
}
Blip类:
using UnityEngine;
public class Blip : MonoBehaviour
{
private MiniMap map;
private RectTransform myRectTransform;
public bool KeepInBounds = true;
public bool LockScale = false;
public Transform Target;
private void Start()
{
map = GetComponentInParent<MiniMap>();
myRectTransform = GetComponent<RectTransform>();
var rectTransformMap = GetComponentInParent<RectTransform>();
myRectTransform.localPosition = new Vector2((rectTransformMap.rect.width - myRectTransform.rect.width) / 2, (rectTransformMap.rect.height - myRectTransform.rect.height) / 2);
}
private void LateUpdate()
{
Vector2 newPosition = map.TransformPosition(Target.position);
newPosition.x -= myRectTransform.rect.width / 2;
newPosition.y -= myRectTransform.rect.height / 2;
if (KeepInBounds)
newPosition = map.MoveInside(newPosition, this.myRectTransform.rect);
myRectTransform.localPosition = newPosition;
}
}