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 hatchnet · Jun 08, 2012 at 11:24 PM · raycastarray

Waypoint problems

Hi there,

I have an array of waypoints, I am trying to move from point to point in order of the ones visited the longest time ago, I have an array which i cycle through looking for points I can see and then I move my toon toward it, when it gets there, the point is moved to the bottom of the list and I go for the next one. The code is below. Sometime it works and sometimes it return a null even though I know a point is visible. There's a bug there somewhere but I just can't get it. is my ray striking my toon's character controller maybe? any thoughts would be good.

Thanks all

 #pragma strict
 #pragma downcast
 
 var closeEnough : float = 8.0; // distance where we are clo9se enough to a point to go to the next
 private var wayArray : Array; // an arrray of waypoints
 
 function Start()
 {
     // collect all of the waypoints
     wayArray = new Array(GameObject.FindGameObjectsWithTag("WayPoint")); 
 }
 
 function FindOldestWaypoint () : GameObject {
     var hit : RaycastHit;
 
     var oldest: GameObject; 
     var position = transform.position; 
     var lp : int;
     
     
     // Iterate through them and find the top of the list
     for (lp =  0 ; lp < wayArray.length; lp ++)
        {
            var go : GameObject =  wayArray[lp];
            
             // raycast, check for intervening objects, can I see it? 
             var raydirection =  go.transform.position - position;
 
         // if the waypoint we are testing has a collider blocking it the ignore it0
            if (!Physics.Raycast (transform.position, raydirection, hit)) 
         {
             // draw a debug ray
                 Debug.DrawRay(transform.position, raydirection, Color.red);  // debug code
                 var diff = (go.transform.position - position);
                 var curDistance = diff.sqrMagnitude; 
                 
                 if (curDistance < closeEnough) { 
                     // if we are close engough, the current waypoint is moved to the bottom of the list
                     wayArray.RemoveAt(lp);
                     wayArray.Push(go);
                 }
 
             oldest: = wayArray[lp];
             break;
         }
         
     } 
     return oldest: 
 }
Comment
Add comment · Show 2
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 bompi88 · Jun 08, 2012 at 11:52 PM 0
Share

why is it

oldest: = wayArray[lp];
and not?
oldest = wayArray[lp];

And "return oldest:" should be "return oldest;". Another thing: format your code in the question with selecting all of your code and hit 101010, till it formats well. Then I will take a closer look at it.

avatar image asafsitner · Jun 09, 2012 at 09:05 AM 0
Share

Not sure this is what causes the problem, but you might as well fix it while you're at it - normalize your ray direction vector, or use it's normalized property within the raycast call.

You can get rid of 'diff' as you've already calculated the same vector for the raycast direction, so you can use that ins$$anonymous$$d.

You might also want to reference the oldest point before changing the array.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hatchnet · Jun 09, 2012 at 11:04 PM

Thanks for the suggestions guys, the recalculated vector was an oversight, the : in the wrong place seems to have been because of the way I copied and pasted the code.

I solved the problem by placing the playing surface on the ignore raycast surface, a new concept for my newbie self. I found the problem by putting lots of debug statements in to find out what the ray was colliding with and hey presto, it was the playing surface.

Oh, and I didn't know about the 101010 button. Here is my completed code

Cheers

 function FindOldestWaypoint () : GameObject {
 var hit : RaycastHit;

 var oldest : GameObject; 
 var distance = Mathf.Infinity; 
 var position = transform.position; 
 var lp : int;
 
 
 // Iterate through them and find the top of the list
 for (lp =  0 ; lp < wayArray.length; lp ++)
    {

        var go : GameObject =  wayArray[lp];
        
     // raycast, can I see it?
     var raydirection =  go.transform.position - position;
     Physics.Raycast (transform.position, raydirection, hit);

     // if the waypoint we are testing has a collider blocking it the ignore it0
        if (!Physics.Raycast (transform.position, raydirection, hit)) 
     {
     
             // draw a debug ray
             Debug.DrawRay(transform.position, raydirection, Color.red);  // debug code
             
             // get the distance to the waypoint and if it's close, move it to the end of the array 
             // and go for the neat one
             var curDistance = raydirection.sqrMagnitude; 
             
             if (curDistance < closeEnough) { 
             
                 // if we are close engough, the current waypoint is deleted and 
                 // moved to the bottom of the list
                 wayArray.RemoveAt(lp);
                 wayArray.Push(go);
             }
             else
             {
                 oldest = wayArray[lp];
                 break;
             }
     }
     
 } 
 return oldest;

}

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

Raycast cycle problem? 0 Answers

Array index is out of range and Raycast question 0 Answers

Return a List of Gameobjects at the mouse position 0 Answers

C# adding items to an already created Array 4 Answers

Hover Effect with more rays as turbines? 1 Answer


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