- Home /
Problem is not reproducible or outdated
Offscreen target indicator- how to do it???
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);
}
}
}
Answer by yaourt · Oct 11, 2018 at 11:21 AM
Many ways to do this. One could be comparing camera position + size x/y and enemy's position to know if it's out of view, you can then return a value to know if the enemy is on top/right/bottom/left side and update arrow's position on x or y... Not sure if it has any impact but it might be cleaner to check if your child is not active before activating it on each frame even if I have no idea what is your player's children...
Follow this Question
Related Questions
Drawing view port slightly off screen 0 Answers
How to make camera position relative to a specific target. 1 Answer
2D Sprite rotation but still face the camera? 0 Answers
OffScreen Effect 1 Answer