I posted this in the Programming OT just now but might as well ask here as well.
I've been trying to make a train move in certain paths with something called iTween in Unity for a while now but I can't really get my head around it :lol I'm using the second one,
iTween, but a visual scripting tool plugin version of it instead, found both in the asset store. I've set up three individual rudimentary script animations and one of them plays automatically just fine. I'm not sure if this is a good way of doing it but coding a branching path seems even more advanced.
I decided that the moving object could deal with the controller aspect and I've set up a working bool switcher:
Code:
public class TrainController : MonoBehaviour
{
public bool changedDirection = false;
void Update()
{
if(Input.GetKeyDown("space") && changedDirection == false)
{
Debug.Log("success");
changedDirection = true;
}
else if(Input.GetKeyDown("space") && changedDirection == true)
{
Debug.Log("reverted");
changedDirection = false;
}
}
}
The idea is to let the player switch a bool and when the object collides with a trigger box, that box will check the bool status and that way continue playing one of the other two animations:
Code:
public class TrainTracker : MonoBehaviour
{
public TrainController trainController;
void OnTriggerEnter(Collider other)
{
if(trainController.changedDirection == false)
{
//iTweenEvent.GetEvent(GameObject.FindGameObjectWithTag("trainCart"), "Forward_Crash");
Debug.Log("false");
}
else if(trainController.changedDirection == true)
{
//*insert second animation reference here*
Debug.Log("true");
}
}
}
This Debug.Log() code isn't working so I assume connecting an animation to it won't work either for some reason. I also have no idea how to reference the two separate animations or how to get them to play xD There's supposedly a Play() function but I haven't found it. The tutorials on unity3d.com use tags to find objects all the time so that's what I've been trying here as well, via the script names.
The objects in question have their scripts attached of course, and the train has a trigger box as well. When I made the public TrainController in the TrainTracker script, unity is giving me the option to link something to that slot in the editor so I placed the train prefab in it, not that it made any difference. Not sure if it's important that the train doesn't have a rigidbody at the moment.
TL;DR: I want the player to be able to decide which railroad track a train travels on, either continue forward, or take a turn to the side. I have three animations ready: Forward, left turn, and finally continue forward from the crossroads. Could use some help setting it up, if anyone's used iTween or similar before.
Edit: Adding a rigidbody to the train solved the issue of the trigger noticing the train. Now i just need to play the different animations somehow.
Edit2: It looks like shit but it finally works! The animation code was right all along, just need to add ".Play();" after it...