using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyLook : MonoBehaviour { public Transform player; public float result; public float distance; // Use this for initialization void Start() { ////向量点乘的数学运算 //Vector3 enemy2Player = player.position - transform.position; //Vector3 enemyForward = transform.forward; //result = enemy2Player.x * enemyForward.x + enemy2Player.y * enemyForward.y + enemy2Player.z + enemyForward.z; //把向量变成单位向量 Vector3 enemy2Player = (player.position - transform.position).normalized; Vector3 enemyForward = transform.forward; //计算两个向量之间的夹角 result = Vector3.Dot(enemy2Player, enemyForward); //计算两个物体之间的距离 distance = Vector3.Distance(player.position, transform.position); //API:计算两个向量之间的夹角 //Vector3.Angle(); } // Update is called once per frame void Update() { } private void OnGUI() { //if (result > 0) //{ // GUILayout.Label("在敌人前方"); //} //else //{ // GUILayout.Label("在敌人后方"); //} if (result > Mathf.Cos(30 * Mathf.Deg2Rad) && distance < 5) { GUILayout.Label("在扇形范围内:" + Mathf.Acos(result) * Mathf.Rad2Deg + "度"); } else { GUILayout.Label("不在扇形范围内:" + Mathf.Acos(result) * Mathf.Rad2Deg + "度"); } } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class TestTrans : MonoBehaviour { public Transform Target; public float distance; private float SkillDistance = 5;//扇形距离 private float SkillJiaodu = 60;//扇形的角度 // Use this for initialization void Start () { ////偶然性编程 distance = Vector3.Distance(transform.position, Target.position);//距离 Vector3 norVec = transform.rotation * Vector3.forward; Vector3 temVec = Target.position - transform.position; Debug.DrawLine(transform.position, norVec, Color.red);//画出技能释放者面对的方向向量 Debug.DrawLine(transform.position, Target.position, Color.green);//画出技能释放者与目标点的连线 float jiajiao = Mathf.Acos(Vector3.Dot(norVec.normalized, temVec.normalized)) * Mathf.Rad2Deg; if (distance <= SkillDistance) { if (jiajiao <= SkillJiaodu) { Debug.Log("在扇形范围内"); } else { Debug.Log("不在扇形范围内"); } } } // Update is called once per frame void Update () { } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CrystalMove : MonoBehaviour { //public Transform startTrans; //public float moveSpeed = 1; //public float rotSpeed = 1; public float rotateSpeed = 30; //调节上下频率和浮动 public float amplitude = 1f; public float frequency = 1f; // Use this for initialization void Start () { } // Update is called once per frame void Update () { // transform.Rotate(Vector3.up, Space.World); // Vector3 delta = Vector3.up * Mathf.Sin(Time.time) * moveSpeed*0.25F; // transform.position = startTrans.position + delta; transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed); float sin = Mathf.Sin(Time.time * frequency) * amplitude; transform.Translate(Vector3.up * sin, Space.World); } }