Path-Creator | Path creation asset for Unity game development | Game Engine library
kandi X-RAY | Path-Creator Summary
Support
Quality
Security
License
Reuse
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample Here
Path-Creator Key Features
Path-Creator Examples and Code Snippets
Trending Discussions on Path-Creator
Trending Discussions on Path-Creator
QUESTION
I use the Bezier Path Creator from the package manager to let a train ride on a predefined path. (https://assetstore.unity.com/packages/tools/utilities/b-zier-path-creator-136082)
Currently, I have the following code on one train unit:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class pathFollower : MonoBehaviour
{
public PathCreator pathCreator;
public float speed = 5;
float distanceTravelled;
// Update is called once per frame
void Update()
{
distanceTravelled += speed * Time.deltaTime;
transform.position = pathCreator.path.GetPointAtDistance(distanceTravelled);
transform.rotation = pathCreator.path.GetRotationAtDistance(distanceTravelled);
}
}
This works pretty well. But when I have a train with multiple units/wagons, every wagon needs to turn after the other, not all at once.
I'm a little lost, how I could solve this. Any ideas?
Thanks!
ANSWER
Answered 2021-Apr-08 at 17:25I would handle this by handling the front and back bogie of each car instead of the center of each car. And if you can assume that the track is relatively straight and that each car is going the same speed (these are the same assumption unless you want to model spring forces at each car coupling), this should work:
public class pathFollower : MonoBehaviour
{
public PathCreator pathCreator;
public float speed = 5;
float distanceTravelled;
// set these appropriately in inspector and/or instantiation
public float startDistance = 0f;
public float carLength = 2f;
// Update is called once per frame
void Update()
{
distanceTravelled += speed * Time.deltaTime;
UpdateTransform();
}
// May want to call this when instantiating, after startDistance & carLength are called
// to position it before the first call to Update
public void UpdateTransform()
{
Vector3 rearPos = pathCreator.path.GetPointAtDistance(startDistance + distanceTravelled);
Vector3 frontPos = pathCreator.path.GetPointAtDistance(startDistance + distanceTravelled
+ carLength);
transform.position = 0.5f * (rearPos+frontPos); // midpoint between bogies
transform.LookAt(frontPos);
}
}
be sure to set the startDistance
of each car so that it includes the padding between all previous cars as well as the length of all previous cars, so the caboose has startDistance
=0, the 2nd to last car has startDistance
=cabooseLength + padding, etc..
Note that this will result in different amounts of separation between the cars at different times, which is realistic behavior for a sequence of cars all moving at the same speed along a track.
To reiterate, if you want a constant distance between each car along curves, then you can't assume a common speed among each car.
Ideally, you would have a way to find all intersections among your bezier curves with the sphere centered at a given world position and radius. It's more of a math question but if you had that method, you could use that in a couple places.
Once, for each car to find where to put the front bogey based on the rear bogey using a sphere of radius carLength
and centered at rearPos
. And second, for each successive car, to find where to put the rear bogey based on the front bogy of the previous car, using a sphere of radius padding
centered at the previous car's frontPos
. Alternatively, you could start with the front car and work backwards. And you would need to be sure that you ignore intersections that would have the car "jump" the track and other nonsense.
If you did find this method, each car would want to refer to the front and/or back bogeys of its neighbors, so that would suggest putting in some additional fields.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Path-Creator
Support
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesExplore Kits - Develop, implement, customize Projects, Custom Functions and Applications with kandi kits
Save this library and start creating your kit
Share this Page