• 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
Question by Sondre-S · Feb 24, 2014 at 04:39 PM · javascriptwaypointstraffic

Trying to get paths from container[Solved]

Hi, I'm making a traffic AI script that read gets waypoints from a path object, now I am trying to get a random path to start with from a path container object(in the same way as I get the waypoints), but I get this error: "InvalidCastException: Cannot cast from source type to destination type."

Basically the GetFirstPath() function is supposed to work similar to GetWaypoints().

 // AI Traffic car script //
 ///////////////////////////
 
 var WheelFR: WheelCollider;
 var WheelFL: WheelCollider;
 var WheelRR: WheelCollider;
 var WheelRL: WheelCollider;
 
 var MaxTorque= 15;
 var BrakeTorque=20;
 var MaxAngle=30;
 private var Speed: float;
 private var KPH: float;
 var MaxSpeedKPH=45;
 
 var SteeringAngle;
 var DriveTorque;
 var CenterOfMass=-0.7;
 
 var waypointContainer : GameObject;
 private var waypoints : Array;
 private var currentWaypoint : int;
 private var SpawnWaypoint : int;
 private var FirstPath: int;
 private var NextPathChosen:int;
 
 var pathContainer : GameObject;
  var paths : Array; 
 
 
 private var inputSteer : float = 0.0;
 private var inputTorque : float = 0.0;
 
 private var Obstacle:boolean;
 
 private var Timer:float;
 private var Chosen:boolean;
 
 
 
 function Start () // initializing
 {
 // assign waypoint container
  
 GetFirstPath();
 // pick random path/waypointContainer
 FirstPath = Random.Range(0, (paths.length-1));
 waypointContainer= paths[FirstPath];
 
 
 
 //lower COG
 rigidbody.centerOfMass.y = CenterOfMass;
 // get the waypoints
 GetWaypoints();
 
 
 }
 
 // MAIN 
 
 function Update ()
 {


   
 }
 
 // WAYPOINTS
 
 function FixedUpdate () {
 NavigateTowardsWaypoint();
 }
 
 function GetWaypoints () {
     // Now, this function basically takes the container object for the waypoints, then finds all of the transforms in it,
     // once it has the transforms, it checks to make sure it's not the container, and adds them to the array of waypoints.
     var potentialWaypoints : Array = waypointContainer.GetComponentsInChildren( Transform );
     waypoints = new Array();
     
     for ( var potentialWaypoint : Transform in potentialWaypoints ) {
         if ( potentialWaypoint != waypointContainer.transform ) {
             waypoints[ waypoints.length ] = potentialWaypoint;
         }
     }
 }
 
 
 function GetFirstPath () {
     // get the first path from a path container
     var potentialPaths : Array = pathContainer.GetComponentsInChildren( Transform );
     paths = new Array();
     
     for ( var potentialPath : Transform in potentialPaths ) {
         if ( potentialPath != pathContainer.transform ) {
             paths[ paths.length ] = potentialPath;
         }
     }
 }
 
 function NavigateTowardsWaypoint () {
     // now we just find the relative position of the waypoint from the car transform,
     // that way we can determine how far to the left and right the waypoint is.
     var RelativeWaypointPosition : Vector3 = transform.InverseTransformPoint( Vector3(waypoints[currentWaypoint].position.x, transform.position.y, waypoints[currentWaypoint].position.z ) );
                                                                                 
                                                                                 
     // by dividing the horizontal position by the magnitude, we get a decimal percentage of the turn angle that we can use to drive the wheels
     inputSteer = RelativeWaypointPosition.x / RelativeWaypointPosition.magnitude;
     
     // now we do the same for torque, but make sure that it doesn't apply any engine torque when going around a sharp turn...
     if ( Mathf.Abs( inputSteer ) < 0.5 && !Obstacle) {
         inputTorque = 0.5; //RelativeWaypointPosition.z / RelativeWaypointPosition.magnitude - Mathf.Abs( inputSteer );
     }
     if (Mathf.Abs( inputSteer ) > 0.5 || Obstacle){
         //inputTorque = 0;
     }
     
     // this just checks if the car's position is near enough to a waypoint to count as passing it, if it is, then change the target waypoint to the
     // next in the list.
     if ( RelativeWaypointPosition.magnitude < 1 ) {
         currentWaypoint ++;
         
         //if ( currentWaypoint < waypoints.length ) {
             
             
         //}
         
         
         
         //
     }
     
     
     
 }
 
 
 
 
Comment

People who like this

0 Show 5
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 karlhulme · Feb 24, 2014 at 04:49 PM 0
Share

Please state on which line the InvalidCastException is thrown.

avatar image RudyTheDev · Feb 24, 2014 at 05:01 PM 0
Share

See this question/answer. You need to use .GetComponentsInChildren.< Transform >() otherwise it returns the base type Component.

avatar image Sondre-S RudyTheDev · Feb 24, 2014 at 05:29 PM 0
Share

What do you mean exactly? This is Java script and the GetWayorints function is exactly the same but it works correctly...

avatar image Sondre-S · Feb 24, 2014 at 05:26 PM 0
Share

it's line 48, waypointContainer= paths[FirstPath];

avatar image RudyTheDev · Feb 24, 2014 at 06:14 PM 0
Share

@Sondre.S: Converted answer to comment, as I realize now this wasn't where your issue was. Though I'm still curious as to if that part actually works.

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by Sondre-S · Feb 24, 2014 at 06:54 PM

Solved it! The problem was this line:

 paths[ paths.length ] = potentialPath;

just changed it to:

 paths[ paths.length ] = potentialPath.gameObject;

to fill the array with game objects instead of transforms.

The only problem now is that it also adds child objects(the actual waypoints) of the waypoint containers, this way it may set one of the waypoints as waypointcontainer... how can I prevent this from happening?

edit: solved it by checking if the childrens parent transforom is the waypoint container:

if ( potentialPath != pathContainer.gameObject && potentialPath.transform.parent == pathContainer.transform) { paths[ paths.length ] = potentialPath.gameObject; // fill the array with only level 1 children }

Comment

People who like this

0 Show 0 · 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

Answer by RudyTheDev · Feb 24, 2014 at 06:13 PM

Your var waypointContainer : GameObject; is GameObject, but GetFirstPath() fills your paths array with Transforms. So when you try waypointContainer = paths[FirstPath];, you are assigning a Transform type variable to a GameObject type variable.

You probably want your waypointContainer variable to be of Transform type to fix this, although I'm not sure what it is intended for so you might want something else.

Comment
Sondre-S

People who like this

1 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 Sondre-S · Feb 24, 2014 at 06:42 PM 0
Share

Ok, I see what you mean now, then what I need is to get the game objects that the transforms are attached to...

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Waypoint system trouble. 0 Answers

No reason why this shouldent work 0 Answers

Move at mouse click 0 Answers

Follow up to AI Pathfinding Question 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