Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Exagerate · Mar 11, 2013 at 02:23 PM · c#itweentrainrailitween path

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

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · 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 Exagerate · Mar 11, 2013 at 05:41 PM 0
Share

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.

avatar image Exagerate · Mar 11, 2013 at 05:41 PM 0
Share
     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);
             }        
         }
     }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

iTween, I need a Sharp movement between points on an iTween path 0 Answers

Itween path, how to find the closest point to the player/Vector3 2 Answers

How to control a game object's movement in iTween 1 Answer


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