①卡牌高度提升
②卡牌旋转归零
③卡牌层级高于其他手牌
①拖拽超过一定高度之后卡牌会移动到手牌中心位置
②出现攻击引导箭头
③成功指向目标怪物后打出
①可自由拖动
②脱离手牌高度后打出
①卡牌从当前位置移动到中心位置
②卡牌居中悬停展示几毫秒
③播放回归弃牌堆动画
核心代码
public class CardItem : MonoBehaviour { /// /// 卡牌扇形展开中心点 /// public Vector3 root; /// /// 展开角度 /// public float rot; /// /// 展开半径 /// public float size; /// /// 动画速度 /// public float animSpeed = 10; /// /// 高度值(决定卡牌层级) /// public float zPos=0; public void RefreshData(Vector3 root,float rot, float size,float zPos) { this.root = root; this.rot = rot; this.size = size; this.zPos = -zPos*0.02f; } // Update is called once per frame void Update() { SetPos(); } public void SetPos() { //设置卡牌位置 float x = root.x + Mathf.Cos(rot) * size; float y = root.y + Mathf.Sin(rot) * size; transform.position = Vector3.Lerp(transform.position, new Vector3(x, y, root.z+this.zPos), Time.deltaTime * animSpeed); //设置卡牌角度 float rotZ = GetAngleInDegrees(root, transform.position); Vector3 localEulerAngles = transform.localEulerAngles; Quaternion rotationQuaternion = Quaternion.Euler(new Vector3(0, 0, rotZ)); transform.rotation = Quaternion.RotateTowards(transform.rotation, rotationQuaternion, Time.deltaTime * animSpeed * 30); } /// /// 获取两个向量之间的弧度值0-2π /// /// 点A坐标 /// 点B坐标 /// public static float GetAngleInDegrees(Vector3 positionA, Vector3 positionB) { // 计算从A指向B的向量 Vector3 direction = positionB - positionA; // 将向量标准化 Vector3 normalizedDirection = direction.normalized; // 计算夹角的弧度值 float dotProduct = Vector3.Dot(normalizedDirection, Vector3.up); float angleInRadians = Mathf.Acos(dotProduct); //判断夹角的方向:通过计算一个参考向量与两个物体之间的叉乘,可以确定夹角是顺时针还是逆时针方向。这将帮助我们将夹角的范围扩展到0到360度。 Vector3 cross = Vector3.Cross(normalizedDirection, Vector3.up); if (cross.z > 0) { angleInRadians = 2 * Mathf.PI - angleInRadians; } // 将弧度值转换为角度值 float angleInDegrees = angleInRadians * Mathf.Rad2Deg; return angleInDegrees; }}
public class CardManager : MonoBehaviour { /// /// 卡牌起始位置 /// public Vector3 rootPos=new Vector3(0,-33.5f,20); /// /// 卡牌对象 /// public GameObject cardItem; /// /// 扇形半径 /// public float size =30f; /// /// 卡牌出现最大位置 /// private float minPos = 1.415f; /// /// 卡牌出现最小位置 /// private float maxPos = 1.73f; /// /// 手牌列表 /// private List cardList; /// /// 手牌位置 /// private List rotPos; /// /// 最大手牌数量 /// private int CardMaxCount=8; void Start() { InitCard(); } /// /// 数据初始化 /// public void InitCard() { rotPos=InitRotPos(CardMaxCount); } /// /// 初始化位置 /// /// /// /// public List InitRotPos(int count) { List rotPos=new List(); float interval = (maxPos - minPos)/count; for (int i = 0; i < count; i++) { float nowPos = maxPos - interval * i; rotPos.Add(nowPos); } return rotPos; } // Update is called once per frame void Update() { TaskItemDetection(); RefereshCard(); } /// /// 添加卡牌 /// public void AddCard() { if (cardList==null) { cardList=new List(); } if (cardList.Count>=CardMaxCount) { Debug.Log("手牌数量上限"); return; } GameObject item = Instantiate(cardItem,this.transform); CardItem text=item.GetComponent(); text.RefreshData(rootPos,0,0,0); cardList.Add(text); } /// /// 手牌状态刷新 /// public void RefereshCard() { if (cardList==null) { return; } for (int i = 0; i < cardList.Count; i++) { cardList[i].RefreshData(rootPos,rotPos[i],size,i); } } /// /// 销毁卡牌 /// public void RemoveCard() { if (cardList==null) { return; } CardItem item = cardList[cardList.Count - 1]; cardList.Remove(item); Destroy(item.gameObject); } /// /// 销毁卡牌 /// /// public void RemoveCard(CardItem item) { if (cardList==null) { return; } cardList.Remove(item); Destroy(item.gameObject); } private Vector3 oldmousePosition; /// /// 玩家操作检测 /// public void TaskItemDetection() { if (Input.GetKeyDown(KeyCode.A)) { AddCard(); } } }