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 thieum · Jan 30, 2012 at 02:31 PM · arraywaypoint systemreturn value

[solved] c# script : get a return val (waypoint example)

Hello all,

I need to increase my c# knowledge, I can't really understand how static and return values are meant to be declared...

The following script uses a simple waypoints system, really cool as it doesn't require to assign waypoints manually, nore to set the array count, but just picks up all the transforms from a given game object. :°D

Now I'm attempting to get the closest waypoint from the array list. Array is working properly. tmpDist and Dist are correct too... But the "FindClosest" function is not working yet. ;°(

I guess I need a little explanation about how to deal with array items :°p

  • Should the FindClosest function be static or void?

  • what is to be declared within brackets in the function FindClosest ()?

Thanks in advance for your help...


 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 
 public class Waypoints : MonoBehaviour {
 
     public Transform myTransform;
     
     public GameObject waypointContainer;
     List <Transform> waypoints ;
     Transform[] potentialWaypoints;
     public float Dist;
     public float tmpDist;
     public int bestWaypoint;
     public int currentWaypoint;
     public float currentAngle;
     Vector3 RelativeWaypointPosition;
     Vector3 RelativeWaypointDir;
 
 
 //////////////////////////// waypoints ///////////////////////
 
 
     void Start ()
     {
         GetWaypoints();
         FindClosest(); ///// tried this (bestWaypoint); //(potentialWaypoints[]);
         currentWaypoint = bestWaypoint;
     }
     
     void GetWaypoints ()
     {
         if (waypointContainer!=null) 
         {
             Transform[] potentialWaypoints = waypointContainer.GetComponentsInChildren <Transform> ();
 
             waypoints = new List <Transform> ();
 
             foreach (Transform potentialWaypoint in potentialWaypoints) 
             {
                 for (int i=0;i<waypoints.Count;i++);
                 if (potentialWaypoint != waypointContainer.transform)
                     waypoints.Add (potentialWaypoint);
             //    Debug.Log("foundpotentialWaypoint");
             }
         }
     }    
         
     void FindClosest () {  /////tried this: (Transform[] waypoints) {
         
         Dist = (waypoints[0].transform.position - transform.position).sqrMagnitude;
         waypoints[bestWaypoint] = waypoints[0];
             
         for (int i=1;i<waypoints.Count;i++) {
             tmpDist = (waypoints[i].transform.position - transform.position).sqrMagnitude;
             if (tmpDist < Dist)
                 waypoints[bestWaypoint] = waypoints[i];
         }
 //        return waypoints[bestWaypoint];
     }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by mpavlinsky · Jan 30, 2012 at 02:49 PM

You cannot have the FindClosest function be static because it operates on non-static members.

It seems odd to me that you are shuffling around the waypoints in the FindClosest function. You are always inadvertently overwriting the first waypoint in the list with one of the other ones whenever a waypoint is found to be closer. Instead you might want to use bestWaypoint to be the index of the best waypoint and not just be basically a constant zero moving the best waypoint into that index. Example:

    void FindClosest () {

        Dist = (waypoints[0].transform.position - transform.position).sqrMagnitude;
        bestWaypoint = 0;

        for (int i=1;i<waypoints.Count;i++) {
          tmpDist = (waypoints[i].transform.position - transform.position).sqrMagnitude;
          if (tmpDist < Dist)
           bestWaypoint = i;
        }
     }

Comment
Add comment · 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
0

Answer by thieum · Jan 30, 2012 at 05:20 PM

Thanks for your reply.

I did try this option, but I get a strange result : In my array configuration, myTransform stands nearby WP n°5 but FindClosest function returns WP n° 401 as the closest. As WP n° 401 is the last one in the loop, it is actually the closest to WP n°0... so it looks like the FindClosest returns the closest to WP n°0 instead of the closest to myTransform... If I move WP n°0 next to WP n°400, then the closest WP found is the n°400.

I updated this btw, but it has no further effect:

tmpDist = (Waypoints[i].transform.position - myTransform.position).sqrMagnitude;

So I guess I'm not far from the solution, but I still wonder why... uh... cruel world...

Thanks again for your kind attnetion and compassion for newbies ;°)

///////////// edit

Well, I found the fix: I forgot to assign the Dist = tmpDist as it can update the lowest item found in the array... Obvious. :°D

Here is the updated FindClosest function. I hope it can be usefull to other, as it's quite rare to find c# waypoints system, mainly js...

void FindClosest () { Dist = Mathf.Infinity; // Dist = (Waypoints[0].transform.position - myTransform.position).sqrMagnitude;

     for (int i=0;i&lt;Waypoints.Count;i++) {
         tmpDist = (Waypoints[i].transform.position - myTransform.position).sqrMagnitude;
         if (tmpDist &lt; Dist) {
             Dist = tmpDist;
             bestWaypoint = i;
         }
     }
 }

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Return array of strings 1 Answer

Array of bool doesn't change in other script 1 Answer

How to create a 2d array of a class 1 Answer

Questions about a Weapon Switch script. 1 Answer

BEST way to get velocity Without rigidbody? 2 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