怪物攻击角色时,UI上箭头执行怪物的方向
2016-08-15 16:11
106 查看
下面是插件的原码:
using UnityEngine;
using System.Collections;
public class ArrowSet : MonoBehaviour
{
public Texture arrow;
private Camera cam;
private float xCenter;
private float yCenter;
private float screenSlope;
private float halfSize;
public float size = 30;
private bool errorless = false;
public string tagToFind = "Enemy";
public float hoverAngle = 270;
public float distanceAbove = 30;
public float blindSpot = 5.0f;
public bool hoverOnScreen = true;
void Start()
{
Camera theCam = gameObject.GetComponent<Camera>();
if (gameObject.GetComponent<Camera>())
{
cam = theCam;
if (arrow != null)
{
errorless = true;
}
else
{
Debug.Log("请附上一个箭头纹理雷达箭脚本。");
errorless = false;
}
}
else
{
Debug.Log("雷达箭头必须连接到一个摄像机。");
}
}
void OnGUI()
{
if (Event.current.type.Equals(EventType.Repaint) && errorless)
{
xCenter = cam.pixelWidth / 2;
yCenter = cam.pixelHeight / 2;
screenSlope = ((float)cam.pixelHeight) / cam.pixelWidth;
halfSize = size / 2;
GameObject[] objects = GameObject.FindGameObjectsWithTag(tagToFind);
foreach (GameObject ob in objects)
{
float angle = hoverAngle - 180;
double rad = angle * (Mathf.PI / 180.0);
Vector3 arrowPos = cam.transform.right * Mathf.Cos((float)rad) + cam.transform.up * Mathf.Sin((float)rad);
Vector3 worldPos = ob.transform.position + (arrowPos * distanceAbove);
//世界转视口
Vector3 pos = cam.WorldToViewportPoint(worldPos);
//Debug.Log(pos);
if (pos.z < 0)
{
pos.x *= -1;
pos.y *= -1;
}
if (pos.z > 0 || (pos.z < 0 && (pos.x > 0.5f + (blindSpot / 2) || pos.x < 0.5f - (blindSpot / 2)) && (pos.y < 0.5f - (blindSpot / 2) || pos.y > 0.5f + (blindSpot / 2))))
{
//找到新的位置
float newX = pos.x * cam.pixelWidth;
//相机坐标垂直翻转相比,像素坐标
float newY = cam.pixelHeight - pos.y * cam.pixelHeight;
////如果对象是屏幕外
if (pos.z < 0 || newY < 0 || newY > cam.pixelHeight || newX < 0 || newX > cam.pixelWidth)
{
float a = CalculateAngle(cam.pixelWidth / 2, cam.pixelHeight / 2, newX, newY);
Vector2 coord = ProjectToEdge(newX, newY);
GUIUtility.RotateAroundPivot(a, coord);
Graphics.DrawTexture(new Rect(coord.x - halfSize, coord.y - halfSize, size, size), arrow);
GUIUtility.RotateAroundPivot(-a, coord);
}
else
if (hoverOnScreen)
{
Debug.Log(222);
float nh = Mathf.Sin((float)rad) * size;
float nw = Mathf.Cos((float)rad) * size;
//当在屏幕上,只需旋转90度,并绘制
GUIUtility.RotateAroundPivot(-angle + 180, new Vector2(newX + nw, newY - nh));
Graphics.DrawTexture(new Rect(newX + nw, newY - nh - halfSize, size, size), arrow, null);
GUIUtility.RotateAroundPivot(angle - 180, new Vector2(newX + nw, newY - nh));
}
}
}
}
}
float CalculateAn
9c66
gle(float x1, float y1, float x2, float y2)
{
float xDiff = x2 - x1;
float yDiff = y2 - y1;
float rad = Mathf.Atan(yDiff / xDiff);
float deg = rad * 180 / Mathf.PI;
if (xDiff < 0)
{
deg += 180;
}
return deg;
}
Vector2 ProjectToEdge(float x2, float y2)
{
float xDiff = x2 - (cam.pixelWidth / 2);
float yDiff = y2 - (cam.pixelHeight / 2);
float slope = yDiff / xDiff;
Vector2 coord = new Vector2(0, 0);
float ratio;
if (slope > screenSlope || slope < -screenSlope)
{
//project on top/bottom
ratio = (yCenter - halfSize) / yDiff;
if (yDiff < 0)
{
coord.y = halfSize;
ratio *= -1;
}
else coord.y = cam.pixelHeight - halfSize;
coord.x = xCenter + xDiff * ratio;
}
else
{
//project on left/right
ratio = (xCenter - halfSize) / xDiff;
if (xDiff < 0)
{
coord.x = halfSize;
ratio *= -1;
}
else coord.x = cam.pixelWidth - halfSize;
coord.y = yCenter + yDiff * ratio;
}
return coord;
}
}
以前代码在 Unity4.6运行良好,但在Untiy5以后的版本就有BUG,上下方向的怪物好使,左右的不显示UI,经过查找发现Camera.pixelWidth和Camera.pixelHeight不再是float值,而是int类型,我们之需要将它们转成flaot就可以修复这个BUG
screenSlope = ((float)cam.pixelHeight) / cam.pixelWidth;
using UnityEngine;
using System.Collections;
public class ArrowSet : MonoBehaviour
{
public Texture arrow;
private Camera cam;
private float xCenter;
private float yCenter;
private float screenSlope;
private float halfSize;
public float size = 30;
private bool errorless = false;
public string tagToFind = "Enemy";
public float hoverAngle = 270;
public float distanceAbove = 30;
public float blindSpot = 5.0f;
public bool hoverOnScreen = true;
void Start()
{
Camera theCam = gameObject.GetComponent<Camera>();
if (gameObject.GetComponent<Camera>())
{
cam = theCam;
if (arrow != null)
{
errorless = true;
}
else
{
Debug.Log("请附上一个箭头纹理雷达箭脚本。");
errorless = false;
}
}
else
{
Debug.Log("雷达箭头必须连接到一个摄像机。");
}
}
void OnGUI()
{
if (Event.current.type.Equals(EventType.Repaint) && errorless)
{
xCenter = cam.pixelWidth / 2;
yCenter = cam.pixelHeight / 2;
screenSlope = ((float)cam.pixelHeight) / cam.pixelWidth;
halfSize = size / 2;
GameObject[] objects = GameObject.FindGameObjectsWithTag(tagToFind);
foreach (GameObject ob in objects)
{
float angle = hoverAngle - 180;
double rad = angle * (Mathf.PI / 180.0);
Vector3 arrowPos = cam.transform.right * Mathf.Cos((float)rad) + cam.transform.up * Mathf.Sin((float)rad);
Vector3 worldPos = ob.transform.position + (arrowPos * distanceAbove);
//世界转视口
Vector3 pos = cam.WorldToViewportPoint(worldPos);
//Debug.Log(pos);
if (pos.z < 0)
{
pos.x *= -1;
pos.y *= -1;
}
if (pos.z > 0 || (pos.z < 0 && (pos.x > 0.5f + (blindSpot / 2) || pos.x < 0.5f - (blindSpot / 2)) && (pos.y < 0.5f - (blindSpot / 2) || pos.y > 0.5f + (blindSpot / 2))))
{
//找到新的位置
float newX = pos.x * cam.pixelWidth;
//相机坐标垂直翻转相比,像素坐标
float newY = cam.pixelHeight - pos.y * cam.pixelHeight;
////如果对象是屏幕外
if (pos.z < 0 || newY < 0 || newY > cam.pixelHeight || newX < 0 || newX > cam.pixelWidth)
{
float a = CalculateAngle(cam.pixelWidth / 2, cam.pixelHeight / 2, newX, newY);
Vector2 coord = ProjectToEdge(newX, newY);
GUIUtility.RotateAroundPivot(a, coord);
Graphics.DrawTexture(new Rect(coord.x - halfSize, coord.y - halfSize, size, size), arrow);
GUIUtility.RotateAroundPivot(-a, coord);
}
else
if (hoverOnScreen)
{
Debug.Log(222);
float nh = Mathf.Sin((float)rad) * size;
float nw = Mathf.Cos((float)rad) * size;
//当在屏幕上,只需旋转90度,并绘制
GUIUtility.RotateAroundPivot(-angle + 180, new Vector2(newX + nw, newY - nh));
Graphics.DrawTexture(new Rect(newX + nw, newY - nh - halfSize, size, size), arrow, null);
GUIUtility.RotateAroundPivot(angle - 180, new Vector2(newX + nw, newY - nh));
}
}
}
}
}
float CalculateAn
9c66
gle(float x1, float y1, float x2, float y2)
{
float xDiff = x2 - x1;
float yDiff = y2 - y1;
float rad = Mathf.Atan(yDiff / xDiff);
float deg = rad * 180 / Mathf.PI;
if (xDiff < 0)
{
deg += 180;
}
return deg;
}
Vector2 ProjectToEdge(float x2, float y2)
{
float xDiff = x2 - (cam.pixelWidth / 2);
float yDiff = y2 - (cam.pixelHeight / 2);
float slope = yDiff / xDiff;
Vector2 coord = new Vector2(0, 0);
float ratio;
if (slope > screenSlope || slope < -screenSlope)
{
//project on top/bottom
ratio = (yCenter - halfSize) / yDiff;
if (yDiff < 0)
{
coord.y = halfSize;
ratio *= -1;
}
else coord.y = cam.pixelHeight - halfSize;
coord.x = xCenter + xDiff * ratio;
}
else
{
//project on left/right
ratio = (xCenter - halfSize) / xDiff;
if (xDiff < 0)
{
coord.x = halfSize;
ratio *= -1;
}
else coord.x = cam.pixelWidth - halfSize;
coord.y = yCenter + yDiff * ratio;
}
return coord;
}
}
以前代码在 Unity4.6运行良好,但在Untiy5以后的版本就有BUG,上下方向的怪物好使,左右的不显示UI,经过查找发现Camera.pixelWidth和Camera.pixelHeight不再是float值,而是int类型,我们之需要将它们转成flaot就可以修复这个BUG
screenSlope = ((float)cam.pixelHeight) / cam.pixelWidth;
相关文章推荐
- 国产dwz jui使用心得
- vue-resource
- UIImage边缘逐渐透明效果
- 2016DEF CON展示:可社交的电子徽章Queercon
- UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释
- 【第一行代码-Android】学习(一)及在studio的迁移(3)UI
- leetCode 51. N-Queens | 回溯问题(N皇后问题) | hard
- UITableView隐藏多余的分割线
- leetcode-java-95. Unique Binary Search Trees II
- 往class="easyui-combobox"的select中添加onchange事件
- ${pageContext.request.contextPath}无效
- new SqlSessionFactoryBuilder().build(inputStream, properties)
- 原型图工具功能比拼1
- some javascrpit quiz
- ERROR 1292 (22007): Truncated incorrect DOUBLE value: '96133dby11'
- 百度UEditor在线编辑器的配置和图片上传
- 【HD 1242】(重写/模板)Rescue
- UICollectionView 布局策略
- 信号发送函数sigqueue和信号安装函数sigaction
- sigqueue函数 进程间通信 信号的发送携带数据