Помогите разобраться с радаром. Взял за основу скрипт с забугорного ресурса http://wiki.unity3d.com/index.php?title=Radar. Но мне не нужна трехмерная среда, у меня используются всего две оси - X и Y.
В геометрии разбираюсь весьма туго еще со школы, учусь ей сейчас в процессе. Вот мой кусок кода, отвечающий за расчеты положений точек на радаре, он не дает точных значений (приблизительные весьма):
Синтаксис:
Используется csharp
using UnityEngine;
using System.Collections;
using System;
public class Radar : MonoBehaviour {
public Texture eTexture; // текстура иконок на радаре
public Texture radarBG; // текстура самого радара
private OTSprite playerObj;
public float mapScale = 0.3f;
public float mapSizePercent = 15;
private float maxDist = 200;
private float mapWidth;
private float mapHeight;
private Vector2 mapCenter; // центр на радаре
private float bX, bY;
// Use this for initialization
void Start () {
playerObj = GameObject.FindGameObjectWithTag("Player").GetComponent<OTSprite>();
setMapLocation();
}
void OnGUI () {
GUI.DrawTexture(new Rect(mapCenter.x - mapWidth/2, mapCenter.y - mapHeight/2, mapWidth, mapHeight), radarBG);
// Рисуем точки врагов
DrawBlipsForObjects("Enemy");
}
private void setMapLocation () {
mapWidth = Screen.width * mapSizePercent/100.0f;
mapHeight = mapWidth;
// Центр на радаре
mapCenter = new Vector2(mapWidth/2, Screen.height - mapHeight/2);
}
private void DrawBlipsForObjects(string tag){
GameObject[] gos = GameObject.FindGameObjectsWithTag(tag);
foreach (GameObject go in gos){
drawBlip(go.GetComponent<OTSprite>());
}
}
private void drawBlip(OTSprite go){
Vector2 centerPos = playerObj.position;
Vector2 extPos = go.position;
int kfX = 1;
int kfY = 1;
// Дистанция до объекта
float dist = Vector2.Distance(centerPos, extPos);
// Поправочные коэффициенты
if(centerPos.x > extPos.x) kfX = -1;
if(centerPos.y > extPos.y) kfY = -1;
// Мат. расчеты
float deltay = Vector2.Angle(centerPos, extPos);
float bX = dist * (float)Math.Round(Mathf.Sin(deltay * Mathf.Deg2Rad), 4);
float bY = dist * (float)Math.Round(Mathf.Cos(deltay * Mathf.Deg2Rad), 4);
// Вычисляем положение от центра радара
bX = bX * mapScale * kfX;
bY = bY * mapScale * kfY;
if(dist<= maxDist)
{
GUI.DrawTexture(new Rect(mapCenter.x + bX, mapCenter.y + bY, 4, 4), eTexture);
}
}
}
using System.Collections;
using System;
public class Radar : MonoBehaviour {
public Texture eTexture; // текстура иконок на радаре
public Texture radarBG; // текстура самого радара
private OTSprite playerObj;
public float mapScale = 0.3f;
public float mapSizePercent = 15;
private float maxDist = 200;
private float mapWidth;
private float mapHeight;
private Vector2 mapCenter; // центр на радаре
private float bX, bY;
// Use this for initialization
void Start () {
playerObj = GameObject.FindGameObjectWithTag("Player").GetComponent<OTSprite>();
setMapLocation();
}
void OnGUI () {
GUI.DrawTexture(new Rect(mapCenter.x - mapWidth/2, mapCenter.y - mapHeight/2, mapWidth, mapHeight), radarBG);
// Рисуем точки врагов
DrawBlipsForObjects("Enemy");
}
private void setMapLocation () {
mapWidth = Screen.width * mapSizePercent/100.0f;
mapHeight = mapWidth;
// Центр на радаре
mapCenter = new Vector2(mapWidth/2, Screen.height - mapHeight/2);
}
private void DrawBlipsForObjects(string tag){
GameObject[] gos = GameObject.FindGameObjectsWithTag(tag);
foreach (GameObject go in gos){
drawBlip(go.GetComponent<OTSprite>());
}
}
private void drawBlip(OTSprite go){
Vector2 centerPos = playerObj.position;
Vector2 extPos = go.position;
int kfX = 1;
int kfY = 1;
// Дистанция до объекта
float dist = Vector2.Distance(centerPos, extPos);
// Поправочные коэффициенты
if(centerPos.x > extPos.x) kfX = -1;
if(centerPos.y > extPos.y) kfY = -1;
// Мат. расчеты
float deltay = Vector2.Angle(centerPos, extPos);
float bX = dist * (float)Math.Round(Mathf.Sin(deltay * Mathf.Deg2Rad), 4);
float bY = dist * (float)Math.Round(Mathf.Cos(deltay * Mathf.Deg2Rad), 4);
// Вычисляем положение от центра радара
bX = bX * mapScale * kfX;
bY = bY * mapScale * kfY;
if(dist<= maxDist)
{
GUI.DrawTexture(new Rect(mapCenter.x + bX, mapCenter.y + bY, 4, 4), eTexture);
}
}
}
Помогите если и не кодом, то хотя бы технологией - куда копать, что читать!