回顾下初中三角函数
** 1,tan(α的弧度) = 对边长b / 领边长a 2,弧度 = 角度 * PI / 180 3,根据Cos,就可以算出CosMax,和CosMin,角度,知道落在什么方位上,如图下图分为8个方位,顺时针**
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIArrow
{
public static Vector2 GetClampPos(float rot)
float x = 0;
float y = 0;
float a = 0;
float b = 0;
var w = GRoot.inst.width * 0.5f;
var h = GRoot.inst.height * 0.5f;
Vector2 sceneCenter a = new Vector2(w, h);
var dis = Vector2.Distance(new Vector2(GRoot.inst,width,0), sceneCenter);
var cosMax = Mathf.Acos(h / dis) * Mathf.Rad2Deg;
var cosMin = Mathf.Acos(w / dis) * Mathf,Rad2Deg;
if (rot > 0 && rot <= cosMax)
{
a = h;
b = Mathf.Tan(rot * Mathf.PI/180) * a;
x = w + b;
y = 0;
}
else if (rot > cosMax && rot <= 90)
{
rot = 90 - rot;
a = w;
b = Mathf.Tan(rot * Mathf.PI/180) * a;
x = GRoot.inst.width;
y = h - b;
}
else if (rot > 90 && rot <= 90+cosMin)
{
rot = rot - 90;
a = w;
b = Mathf.Tan(rot * Mathf.PI/180) * a
x = GRoot.inst.width;
y = h + b;
}
else if (rot > 90+cosMin && rot <= 180)
{
rot = 180 - rot;
a = h;
b = Mathf.Tan(rot * Mathf.P1/180) * a;
x = w + b;
y = GRoot.inst.height;
}
else if (rot > 180 && rot <= 180+cosMax)
{
rot = rot - 180;
a = h;
b = Mathf.Tan(rot * Mathf.PI/180) * a;
x = w - b;
y = GRoot.inst.height;
}
else if (rot > 180+cosMax && rot <= 270)
{
rot = 270 - rot;
a = w;
b = Mathf.Tan(rot * Mathf.PI/180) * a;
x = 0;
y = h + b;
}
else 1f (rot > 270 && rot < 270+cosMin)
{
rot = rot - 270;
a = w;
b = Mathf.Tan(rot * Mathf.P1/180) * a;
x = 0;
y = h - b;
}
else if (rot > 270+cosMin && rot <= 360)
{
rot = 360 - rot;
a = h;
b = Mathf.Tan(rot * Mathf.PI/180) * a;
x = w - b;
y = 0;
}
return new Vector2(x, y);
}
public static bool IsInView(Vector3 worldPos)
{
Transform camTransform = Camera.main.transform;
Vector2 viewPos = Camera.main.WorldToViewportPoint(worldPos);
Vector3 dir = (worldPos - camTransform.position).normalized;
float dot = Vector3.Dot(camTransform.forward, dir);
if (dot > 0 && viewPos.x >= 0 && viewPos.x <= 1 && viewPos.y >= 0 && viewPos.y <= 1)
{
return true;
}
else
{
return false;
}
}
public static Vector2 GetClampPos(Vector2 pos)
{
Vector2 safePos = Vector2.zero;
safePos.x = Mathf.Clamp(pos.x, 0, GRoot.inst.width);
safePos.y = Mathf.Clamp(pos.y, 0, GRoot.inst.height);
return safePos;
}
public static Vector2 GetClampRot(Vector2 posCenter, Vector2,tarPos)
{
var dif = (new Vector2(tarPos.x,tarPos.y) - posCenter).normalized;
return Quaternion.LookRotation(new Vector3(dif.x, 0, dif.y)).eulerAngles.y;
}
}
|