using System.Collections; using System.Collections.Generic; using UnityEngine; //红外线瞄准器的设定 //射线射到的地方会出现红点 public class LaserItem : MonoBehaviour { public GameObject hitImg; RaycastHit laserHit; public float rayDown; // Use this for initialization void Start () { } // Update is called once per frame void Update () { if (Physics.Raycast(gameObject.transform.position + gameObject.transform.forward * 0.2f - gameObject.transform.up * rayDown, gameObject.transform.forward, out laserHit, 10000)) { DrawPoint(laserHit); } } public void DrawPoint(RaycastHit Hit_loaded) { hitImg.transform.position = new Vector3(Hit_loaded.point.x, Hit_loaded.point.y, Hit_loaded.point.z); //Clamp强制取区间的数值,如果不在区间内则取最大/最小值 float angle_x = Mathf.Acos(Mathf.Clamp(Hit_loaded.normal.x, -1f, 1f)) * 180 / Mathf.PI - 90.0f; float angle_y = Mathf.Asin(Mathf.Clamp(Hit_loaded.normal.y, -1f, 1f)) * 180 / Mathf.PI; if (Hit_loaded.normal.z > 0.0f) { angle_x = 180 - angle_x; } //print(new Vector3(angle_y, angle_x, 0.0f)); if (hitImg.transform.eulerAngles.y != 0f) { hitImg.transform.eulerAngles += new Vector3(-angle_y, angle_x, 0.0f); } else { hitImg.transform.eulerAngles += new Vector3(angle_y, angle_x, 0.0f); } } }