I want to create a simple animation of a train that goes from point A to point B and I’ve done that, however I can’t stay on it, because it is “slippery” and the character just falls of.
If I remember correctly, in the old Mafia 1, from year 2002 I think, you could simply walk in inside the train, or the tram and you just ride it, no need to press a button to sit somewhere etc, you don’t slip, there are no bugs, you are standing firmly on the model.
GTA V has the same thing for it’s train, just climb onto it and you won’t fall of.
So how do I do that in the most simple possible way? I suppose that it is something collider related.
Other than trains, this is also probably useful for elevators, ferry boats, cable cars,etc.
Bonus question, how can I make a seat on the train? I tried google, but nothing.
There really is no easy answer to this and, sadly, Unity lacks some of the tools needed to do it properly when using the built in collision. You can try googling ‘moving platforms unity’ and you’ll see lots and lots of threads related to this issue. There are many approaches, they all have their strengths and drawbacks so you’ll have to do some research, weigh your options based on what you need, and start testing things to find out what gives you the result that is most acceptable.
I think the cheap way is to just change the Physics material to something with a high enough friction. All the rigidbodies effected should stick where they are while they are in contact with the train. I’m not sure what happens if a character jumps though.
The next step up is to use parenting to move the two objects together. Detect when the object enters and exits the train, and set the parent Transform accordingly.
I think the most flexible option is to measure the amount the train moved by using LateUpdate, detect all the objects on the train, and move them the same way.
Don’t forget to inform the physics engine that you’re changing the position with Rigidbody.position. Otherwise you lose performance to the colliders recalculating.