- Home /
How do i make multiple waypoints?
Hi guys i manged to make one way point that is attached to a target and it tells the distance between the target and the player
Now im trying to add 5 more and thats what i cant figure out. So im trying to make 6 way points with each target telling the player the distance between the player and target
This is my code
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Waypoint : MonoBehaviour
{
private Image iconImg;
private Text distanceText;
public Transform Player;
public Transform Target;
public Camera Camera;
public float closeDistance;
// Update is called once per frame
private void Start()
{
iconImg = GetComponent<Image>();
distanceText = GetComponentInChildren<Text>();
}
private void Update()
{
if(Target != null)
{
GetDistance();
CheckOnScreen();
}
}
private void GetDistance()
{
float dist = Vector3.Distance(Player.position, Target.position);
distanceText.text = dist.ToString("f1") + "m";
if(dist < closeDistance)
{
Destroy(gameObject);
}
}
private void CheckOnScreen()
{
float thing = Vector3.Dot((Target.position - Camera.transform.position).normalized, Camera.transform.forward);
if(thing <= 0)
{
ToggleUI(false);
}
else
{
ToggleUI(true);
transform.position = Camera.WorldToScreenPoint(Target.position);
}
}
private void ToggleUI(bool _value)
{
iconImg.enabled = _value;
distanceText.enabled = _value;
}
}
Thanks in advance
Answer by SGymme · Apr 11, 2021 at 12:11 PM
Make an Array of GameObjects for your targets
public Transform[] Target = new Transform[6]; //Make an Array for your targets
public Camera Camera;
public float closeDistance;
// Update is called once per frame
private void Start()
{
iconImg = GetComponent<Image>();
distanceText = GetComponentInChildren<Text>();
}
private void Update()
{
int targetCounter = 0; //Loop through the Array and check if Transform is not null
foreach(var target in Target){
if(target != null){
targetCounter++;
}
}
if(targetCounter > 0) //Check if Array is larger than 0
{
GetDistance();
CheckOnScreen();
} else {
Debug.Log("No more targets"); //Array Length is 0
}
}
private void GetDistance()
{
foreach(var target in Target){ //Loop through the Array and check distance tothe waypoints
float dist = Vector3.Distance(Player.position, target.position);
distanceText.text = dist.ToString("f1") + "m";
if(dist < closeDistance)
{
Destroy(gameObject); //If you want to remove the waypoints instead of the gameobject you have to set target to null
}
}
}
private void CheckOnScreen()
{
foreach(var target in Target){ //Loop through the array and check if target is on screen
float thing = Vector3.Dot((target.position - Camera.transform.position).normalized, Camera.transform.forward);
if(thing <= 0)
{
ToggleUI(false);
}
else
{
ToggleUI(true);
transform.position = Camera.WorldToScreenPoint(target.position);
}
}
}
private void ToggleUI(bool _value)
{
iconImg.enabled = _value;
distanceText.enabled = _value;
}
}
Your answer
Follow this Question
Related Questions
Making users able to make custom tracks to follow by the character in the game 0 Answers
How to play an animation with waypoints 1 Answer
How do you move a character to another waypoint after landing on a specific waypoint 1 Answer
Canduct and movement of an enemy 0 Answers
Problem in moving via waypoint 0 Answers