- Home /
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 ) {
//}
//
}
}
Please state on which line the InvalidCastException is thrown.
See this question/answer. You need to use .GetComponentsInChildren.< Transform >()
otherwise it returns the base type Component
.
What do you mean exactly? This is Java script and the GetWayorints function is exactly the same but it works correctly...
@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.
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 }
Answer by RudyTheDev · Feb 24, 2014 at 06:13 PM
Your var waypointContainer : GameObject;
is GameObject
, but GetFirstPath()
fills your paths
array with Transform
s. 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.
Ok, I see what you mean now, then what I need is to get the game objects that the transforms are attached to...
Your answer
![](https://koobas.hobune.stream/wayback/20220613134554im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
No reason why this shouldent work 0 Answers
Move at mouse click 0 Answers
Follow up to AI Pathfinding Question 0 Answers
Waypoints script 1 Answer