- Home /
movement inside a train problem
Hello, i'm having a big problem with a moving platform, well it's not a platform it's a train, wich has this script.
function OnTriggerEnter(col: Collider)
{
if(col.tag == "Player")
{
col.transform.parent = transform.parent;
}
}
function OnTriggerExit(col: Collider)
{
if(col.tag == "Player")
{
col.transform.parent = null;
}
}
this script just parents my player to my train when the player is inside the train, but since i have to use a rigidbody for my player so the game can detect collisions between a sword my character has and enemyes, when my characters gets out of the train he still continues to move with the train, so i don't know what to do, i think it would be nice to have a solution for moving platforms, or in my case trains, that does not require parenting in order to work, but i have no idea on how to use other kinds of solutions.
So anybody knows why my character continues to move when he's outside of the train?
or does anyone knows if there's a better solution on moving platforms, trains or vehicles other than parenting?
Are you really using physics? If not you should make the rigidbody is$$anonymous$$inematic = true.
Answer by Hoeloe · Sep 05, 2013 at 08:05 AM
There is a solution to this. You can add a momentum vector to your player, which is set whenever you touch the ground, and depends on the ground you touch. First of all, assign a rigidbody to the train (kinematic if you don't want actual physics on it). You can then raycast into the ground from your character, and if the character has a rigidbody, use rigidbody.GetPointVelocity(hit.point)
. This gets the current velocity of a specific point on the rigidbody collider. By setting the momentum value equal to this, you will find the player moves along with the train, and by resetting it when you land on other ground, you'll stop that momentum when the player leaves the train.
Thank you for your answer, i kind of understand what you mean, but i have troubles trying to translate this to code, i just don't know how to use rigidbody.GetPointVelocity with a raycast, if you could tell me more about this and a little bit about the code, that would be great. Anyway, thanks for the answer.
You may also be able to set up a Parent Transform Object which contains the train. After every FixedUpdate() frame you could calculate the distance the train traveled between the current frame and the last. Then you could apply this same distance transformation to the player depending on if he is grounded or not or whatever conditions you like?
I've never tried that for something so complicated like a train but it sounds like it should work in theory?
Also if the train moves in a perfectly straight line you could just apply it's current speed variable directly to the player?
Once again I have no idea if this stuff would work I've never done something like this just throwing out some initial thoughts.
@RyanZimmerman87 That's basically the same method I suggested, except with a more accurate way of calculating the speed that would account for more complex motion (acceleration and rotation).
@matpneu Basically, you need a short raycast from the player to the ground. You can then do a quick test for if the ground has a rigidbody attached. If it doesn't, then set a momentum
vector in the player to (0,0,0). If it does, then set the momentum vector to rigidbody.GetPointVelocity(hit.point)
, by using a RaycastHit object, and storing the output data. GetPointVelocity takes a world space coordinate, and returns a velocity vector of that point, according to the current rigidbody. You can easily get the rigidbody of the collider the raycast hit with hit.collider.rigidbody
, so the code you actually need looks something like this:
//Cast a ray and store the information in 'hit'
RaycastHit hit;
if(Physics.Raycast(transform.position, Vector3.down, out hit, 1))
{
//Set the momentum if the ground has a rigidbody
if(hit.collider.rigidbody)
momentum = hit.collider.rigidbody.GetPointVelocity(hit.point);
else //Reset momentum if the ground doesn't have a rigidbody (i.e. isn't moving)
momentum = Vector3.zero;
}
You will also need to define a momentum vector manually, and each frame add it to the player's position.