- Home /
Itween path constraint with multiple vehicles at once
Hi there,
Today I tested the example iTween path-constraint character and I have been testing around and found it very useful for my project (rail vehicles). However, I would like to know how you would have a multi-carriage vehicle where each carriage is restrained to their part of the path respectively. Is there any resources you can point me to, or advice/pointers on doing this?
I guess my question is, how would you make each carriage stick to the path and not de-rail, and attach it via couplings (either realistic or just basic). The cars don't need to be able to seperate, as the scenario will be predefined, so my eight carriage vehicle just needs to move with each carriage as their respective object on the path.
See image below for rail tunnel vehicle example and the path I have conjured up. I've only managed to achieve this for one carriage at a time, which it does drive really nicely with.
http://i.imgur.com/9jDyRhn.png
Kindest regards, Liam
Answer by robertbu · Mar 11, 2013 at 03:21 PM
This question has come across the list a several times in the last couple of months. My suggestion has been to use iTween.PutOnPath() and iTween.PointOnPath() to manually move the cars along the path. You have to set the starting percentages for the each carriage so that they appear to be linked and then you move the percentages by hand in each frame keeping them synchronized. In order to angle them correctly, pick a percentage a bit ahead of the percentage for each car and use PointOnPath() to get the position. Then use Transform.LookAt() to have the car look at the point.
Note there one issue the above does not solve the problem completely. There is no real connection between the cars, so as the carriages round a bend, the connections will tend to pull apart a bit because of the bend. You can mitigate this by making the connections overly large and/or avoiding strong bends.
Hi Robert, to go about this would I make a seperate controller script for each one?, I've shared my controller script as below, this sounds like an exciting solution- so long as I don't have to drag the new path points to each script! That could be a lot of labour each time i'm required to add more points to my rail line! But I guess any solution is a good one.:
To be honest, example scripts/extracts from scripts speak more to me than explanations, being semi-new here, so I wonder if these frequent questions you speak of might have some visual solutions that I can adapt myself.
using UnityEngine;
using System.Collections;
public class Controller : $$anonymous$$onoBehaviour {
public Transform[] controlPath;
public Transform character;
public enum Direction {Forward,Reverse};
private float pathPosition=0;
private RaycastHit hit;
private float speed = 0.05f;
private float rayLength = 5;
private Direction characterDirection;
private Vector3 floorPosition;
private float lookAheadAmount = .01f;
private float ySpeed=0;
void OnDrawGizmos(){
iTween.DrawPath(controlPath,Color.blue);
}
void Start(){
//plop the character pieces in the "Ignore Raycast" layer so we don't have false raycast data:
foreach (Transform child in character) {
child.gameObject.layer=2;
}
}
void Update(){
Detect$$anonymous$$eys();
FindFloorAndRotation();
$$anonymous$$oveCharacter();
}
void Detect$$anonymous$$eys(){
//forward path movement:
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha3)){
characterDirection=Direction.Forward;
}
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Alpha3)) {
pathPosition += Time.deltaTime * speed;
}
//reverse path movement:
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha4)){
characterDirection=Direction.Forward;
}
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Alpha4)) {
//handle path loop around since we can't interpolate a path percentage that's negative(well duh):
float temp = pathPosition - (Time.deltaTime * speed);
if(temp<0){
pathPosition=1;
}else{
pathPosition -= (Time.deltaTime * speed);
}
}
}
void FindFloorAndRotation(){
float pathPercent = pathPosition%1;
Vector3 coordinateOnPath = iTween.PointOnPath(controlPath,pathPercent);
Vector3 lookTarget;
//calculate look data if we aren't going to be looking beyond the extents of the path:
if(pathPercent-lookAheadAmount>=0 && pathPercent+lookAheadAmount <=1){
//leading or trailing point so we can have something to look at:
if(characterDirection==Direction.Forward){
lookTarget = iTween.PointOnPath(controlPath,pathPercent+lookAheadAmount);
}else{
lookTarget = iTween.PointOnPath(controlPath,pathPercent-lookAheadAmount);
}
//look:
character.LookAt(lookTarget);
//nullify all rotations but y since we just want to look where we are going:
float yRot = character.eulerAngles.y;
character.eulerAngles=new Vector3(0,yRot,0);
}
if (Physics.Raycast(coordinateOnPath,-Vector3.up,out hit, rayLength)){
Debug.DrawRay(coordinateOnPath, -Vector3.up * hit.distance);
floorPosition=hit.point;
}
}
void $$anonymous$$oveCharacter(){
//apply gravity:
character.position=new Vector3(floorPosition.x,character.position.y-ySpeed,floorPosition.z);
//floor checking:
if(character.position.y<floorPosition.y){
ySpeed=0;
character.position=new Vector3(floorPosition.x,floorPosition.y,floorPosition.z);
}
}
}