1

I made a minimap, and I'm confused with the camera's. FieldOfView: position of Player on minimap is correct, but the view point of Player(Camera) sees another point dependent of FieldOfView.

MiniMap class:

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 class:

    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;
    }
}
HoloLady
  • 1,041
  • 1
  • 12
  • 28
Knaus Irina
  • 789
  • 5
  • 15
  • 35

0 Answers0