unity 2d 箭頭追蹤

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

using System;

public class ArrowsManager : MonoBehaviour

{

? ? private GameObject m_arrows;

? ? private Vector3 m_Screen_Center;

? ? private void Start()

? ? {

? ? ? ? m_Screen_Center = new Vector3(Screen.width / 2, Screen.height / 2, 0);

? ? ? ? m_arrows = GameObject.Find("Canvas/Arrows");

? ? ? ? InitArrowsColor();

? ? }

? ? void Update()

? ? {

? ? ? ? if (!GameManager.GetInst().m_bStart) return;

? ? ? ? ReflushArrows();? ? ? ?

? ? }

? ? private void ReflushArrows()

? ? {

? ? ? ? for(int i = 0;i < GameManager.GetInst().m_groupManager.m_listEnemyGroup.Count;i++)

? ? ? ? {

? ? ? ? ? ? Group group = GameManager.GetInst().m_groupManager.m_listEnemyGroup[i];

? ? ? ? ? ? string str = Enum.GetName(typeof(E_ClothesType), group.m_clothesType);? ? ? ? ?

? ? ? ? ? ? Transform tfArrow = m_arrows.transform.Find(str);? ? ? ? ?

? ? ? ? ? ? tfArrow.gameObject.SetActive(!IsInScreen(group.transform));

? ? ? ? ? ? SetLookAt(tfArrow.GetComponent<RectTransform>(), group.transform);

? ? ? ? ? ? //設(shè)置距離數(shù)值

? ? ? ? ? ? float distance = Vector3.Distance(group.transform.position, GameManager.GetInst().m_groupManager.m_playerGroup.transform.position);

? ? ? ? ? ? tfArrow.GetChild(0).GetComponent<Text>().text = (int)distance + "m";

? ? ? ? ? ? SetEdge(tfArrow);

? ? ? ? }

? ? }

? ? //設(shè)置到屏幕邊緣

? ? private void SetEdge(Transform tf)

? ? {

? ? ? ? Vector3 CachePosion = tf.right * (Screen.width > Screen.height ? Screen.width - 100 : Screen.height - 100);

? ? ? ? CachePosion.x = Mathf.Clamp(CachePosion.x, -Screen.width / 2 + 100, Screen.width / 2 - 100);

? ? ? ? CachePosion.y = Mathf.Clamp(CachePosion.y, -Screen.height / 2 + 100, Screen.height / 2 - 100);

? ? ? ? tf.GetComponent<RectTransform>().anchoredPosition3D = CachePosion;

? ? }

? ? private void SetLookAt(RectTransform arrow,Transform target)

? ? {

? ? ? ? //將目標(biāo)的世界坐標(biāo)轉(zhuǎn)換成屏幕坐標(biāo)

? ? ? ? Vector3 target_ScreenPoint = Camera.main.WorldToScreenPoint(target.position);

? ? ? ? //計(jì)算target_ScreenPoint和屏幕中心點(diǎn)的絕對值角度

? ? ? ? float angle = Mathf.Atan(Mathf.Abs(target_ScreenPoint.y - Screen.height / 2)

? ? ? ? ? ? / Mathf.Abs(target_ScreenPoint.x - Screen.width / 2)) * 180 / Mathf.PI;

? ? ? ? //通過判斷target_ScreenPoint相對屏幕中心點(diǎn)所在象限處理angle的差值

? ? ? ? if (target_ScreenPoint.x <= Screen.width / 2)

? ? ? ? ? ? angle = target_ScreenPoint.y >= Screen.height / 2 ? 90 - angle : 90 + angle;

? ? ? ? else

? ? ? ? ? ? angle = target_ScreenPoint.y >= Screen.height / 2 ? 270 + angle : 270 - angle;

? ? ? ? #region 計(jì)算攝像機(jī)和目標(biāo)的叉乘

? ? ? ? //以屏幕中心所在的單位向量為起始向量from,并將from的y值設(shè)成和目標(biāo)y值保持一致

? ? ? ? Vector3 from = Camera.main.transform.forward;

? ? ? ? from.y = target.forward.y;

? ? ? ? //將屏幕中心坐標(biāo)轉(zhuǎn)換成世界坐標(biāo)

? ? ? ? Vector3 cameraPos = Camera.main.ScreenToWorldPoint(m_Screen_Center);

? ? ? ? cameraPos.y = target.position.y;

? ? ? ? //求出目標(biāo)點(diǎn)與攝像機(jī)之間的向量

? ? ? ? Vector3 to = target.position - cameraPos;

? ? ? ? //求出兩向量之間的叉乘

? ? ? ? Vector3 cross = Vector3.Cross(from, to);

? ? ? ? #endregion

? ? ? ? //根據(jù)叉乘求出帶符號的角度

? ? ? ? //cross.y > 0:目標(biāo)向量位于起始向量右側(cè)

? ? ? ? //cross.y < 0:目標(biāo)向量位于起始向量左側(cè)

? ? ? ? if (cross.y > 0 && angle < 180)

? ? ? ? ? ? angle += 180;

? ? ? ? else if (cross.y < 0 && angle > 180)

? ? ? ? ? ? angle -= 180;

? ? ? ? Vector3 euler = arrow.eulerAngles;

? ? ? ? euler.z = angle;

? ? ? ? //設(shè)置箭頭的角度

? ? ? ? arrow.eulerAngles = euler;

? ? }

? ? private bool IsInScreen(Transform tf)

? ? {

? ? ? ? Vector2 vt2 = ToScreenPos(tf);? ? ?

? ? ? ? if(vt2.x > 0 && vt2.y > 0 && vt2.x < Screen.width && vt2.y < Screen.height)

? ? ? ? {

? ? ? ? ? ? return true;

? ? ? ? }

? ? ? ? return false;

? ? }

? ? private Vector2 ToScreenPos(Transform tf)

? ? {

? ? ? ? return Camera.main.WorldToScreenPoint(tf.position);

? ? }

? ? private void InitArrowsColor()

? ? {

? ? ? ? for(int i = 0;i < m_arrows.transform.childCount;i++)

? ? ? ? {

? ? ? ? ? ? Transform child = m_arrows.transform.GetChild(i);? ? ? ? ?

? ? ? ? ? ? E_ClothesType type = (E_ClothesType)Enum.Parse(typeof(E_ClothesType), child.name);? ? ? ? ?

? ? ? ? ? ? child.GetComponent<Image>().color = Utils.GetColor(type);

? ? ? ? }

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 講解的 Unity 中幾種不同的坐標(biāo)系與其之間的轉(zhuǎn)換,以及匯總物體的移動(dòng)和旋轉(zhuǎn)方法 本文原地址:Unity學(xué)習(xí)—坐...
    Warl_G閱讀 1,057評論 0 0
  • 一、Unity簡介 1. Unity界面 Shift + Space : 放大界面 Scene界面按鈕渲染模式2D...
    MYves閱讀 8,664評論 0 22
  • 第二部分 1.以下哪一個(gè)選項(xiàng)不屬于Unity引擎所支持的視頻格式文件(D) A.后綴為mov的文件B.后綴為mpg...
    BiLi_Unity閱讀 5,640評論 0 0
  • using System.Reflection; using UnityEngine; // Line drawi...
    GameMobile閱讀 963評論 0 0
  • Unity腳本基礎(chǔ) 1.Unity3D中的協(xié)程(coroutine)和C#線程之間的區(qū)別是什么? 多線程程序同時(shí)運(yùn)...
    豆錚閱讀 3,919評論 0 3

友情鏈接更多精彩內(nèi)容