I’m trying to make a racing game with tracks the player can switch between. Because I wanted to make the track modular I have each track piece hold a Transform which is used to define which is the next piece.
I found Matthew Hughes SeekSteer script and have modified it to suit the purpose. I will mention him in the game credits because that script is awesome
Whilst I get the tracks to pass over their vector data to the players character, using almost identical code I can’t get the Transform to be passed over.
Can anyone see what I’ve missed?
This code is in the track for defining the vectors of the path over it, and also the next track to go to when finished.
var pathpoint1 : Vector3 = Vector3.zero;
// I'll skip the other pathpoint variables because its largely the same.
var pathpoint10 : Vector3 = Vector3.zero;
var forwardtrack : Transform;
function Awake ()
{
pathpoint1.x = transform.position.x;
pathpoint1.y = (transform.position.y + 0.5);
pathpoint1.z = (transform.position.z - 4);
// I'll skip the other pathpoint variables because its largely the same.
pathpoint10.x = transform.position.x;
pathpoint10.y = (transform.position.y + 0.5);
pathpoint10.z = (transform.position.z + 5);
}
function Getpathpoint1 ()
{
return pathpoint1;
}
// I'll skip the other pathpoint variables because its largely the same.
function Getpathpoint10 ()
{
return pathpoint10;
}
function Getforwardtrack ()
{
return forwardtrack;
}
This next bit of code is from the character going down the track. I’ll spare you the bits that I know work.
var straightVarsObject : GameObject = GameObject.Find(currentTrack.name);
nextPointScript = straightVarsObject.GetComponent(StraightVars);
if(currentpathpoint == 10)
{
currenttrack = nextPointScript.Getforwardtrack();
straightVarsObject = GameObject.Find(currentTrack.name);
nextPointScript = straightVarsObject.GetComponent(StraightVars);
currentpathpoint = 1;
}
else
{
currentpathpoint++;
}
if(currentpathpoint == 1)
waypoints = nextPointScript.Getpathpoint1();
// I'll skip the other pathpoint variables because its largely the same.
if(currentpathpoint == 10)
waypoints = nextPointScript.Getpathpoint10();
If anyone can give me some advice I’d really appreciate it, the pathpoints get passed over fine but the forward track doesn’t. I can’t for the life of me see whats wrong.
Where exactly does it go wrong and are you getting an error message? It’s hard to tell what’s wrong with so much code.
A few pointers:
The way you try to get the StraightVars script from the Transform is way too complicated.
– You can just get the game object with currenttrack.gameObject without having to use Find().
– You can use GetComponent() on the Transform, you don’t have to get the game object for that.
i.e. nextPointScript = currenttrack.GetComponent(StraightVars);
Sorry if I didn’t make it clear, the vectors I’ve called ‘pathpoints’ are being passed fine and dandy.
The real problem is that the Transform called 'forwardtrack’is not being passed. This doesn’t create an error message because the code just repeats even if the ‘forwardtrack’ is not changed.
currenttrack = nextPointScript.Getforwardtrack();
This is the code that compiles but doesn’t do what its supposed to. In fact it appears to do nothing.
Xavier, you say you’ve modified the SeekSteer script for your purpose, but could you clarify what you mean by that? The most obvious modification for SeekSteer is to allow it to use an array of Vector3s instead of an array of Transforms - is this what you have done?
Also, is the idea of forwardTrack to contain a reference to the next track? (The chain of objects is known as a “linked list”, incidentally.)
Is there any chance you can post your modified SeekSteer script? I think Adrian’s advice about using arrays to simplify your script is sound - if you can post the script, I can try to fix you up with some code to get you going on this.
Well, if it fails you’ll definitely end up with some kind of error message. If the code gets executed correctly and produces no errors, I would guess it works fine and your problem is elsewhere.
Start with putting a lot of Debug.Log() calls around the area you think goes wrong and check that it gets executed and does what you expect it to.