Hello first of all im new at unity . and i want make my frist game with auto target enemy within the range of my script … here my script
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class FieldOfView : MonoBehaviour {
public float viewRadius;
[Range(0,360)]
public float viewAngle;
public LayerMask targetMask;
public LayerMask obstacleMask;
[HideInInspector]
public List<Transform> visibleTargets = new List<Transform>();
void Start() {
StartCoroutine ("FindTargetsWithDelay", .2f);
}
IEnumerator FindTargetsWithDelay(float delay) {
while (true) {
yield return new WaitForSeconds (delay);
FindVisibleTargets ();
}
}
public void FindVisibleTargets() {
visibleTargets.Clear ();
Collider[] targetsInViewRadius = Physics.OverlapSphere (transform.position, viewRadius, targetMask);
for (int i = 0; i < targetsInViewRadius.Length; i++) {
Transform target = targetsInViewRadius [i].transform;
Vector3 dirToTarget = (target.position - transform.position).normalized;
if (Vector3.Angle (transform.forward, dirToTarget) < viewAngle / 2) {
float dstToTarget = Vector3.Distance (transform.position, target.position);
if (!Physics.Raycast (transform.position, dirToTarget, dstToTarget, obstacleMask)) {
visibleTargets.Add (target);
}
}
}
}
public Vector3 DirFromAngle(float angleInDegrees, bool angleIsGlobal) {
if (!angleIsGlobal) {
angleInDegrees += transform.eulerAngles.y;
}
return new Vector3(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad),0,Mathf.Cos(angleInDegrees * Mathf.Deg2Rad));
}
}
and my question is, how to make my player game object automaticly rotate to the target postition direction if the nearest target inside my angle and reach.
and priority on nearest.
ive got this code from youtube channel. Sebastian Lague dude
i dont really understand how this code work.
if anyone have better and easier code, I gladly followed that
sorry my english not that good