Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Raijinn-sama · Apr 07, 2021 at 07:42 PM · 3dwaypointwaypoints

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
  }
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Raijinn-sama · Apr 11, 2021 at 01:33 PM 0
Share

Thanks !!!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

149 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges