UNITY 2D C#
I wrote a script.
When an object (ENEMY) is within the player’s range, the arrow points to it.
However, I would like the arrow to be just the edge of the camera.
And when the object appears in the camera, it should disappear.
How to do it?
PS. the script is attached to the PLAYER object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TargetIndicator : MonoBehaviour {
public Transform Target;
public float HideDistance;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
var dir = Target.position - transform.position;
if (dir.magnitude < HideDistance) {
SetChildrenActive (false);
}
else{
SetChildrenActive (true);
var angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
void SetChildrenActive (bool value){
foreach (Transform child in transform)
{
child.gameObject.SetActive(value);
}
}
}