I’d love some suggestions for how to use CharacterController in a third person platformer. I’ve spent most of the week creating a third person controller script for a platforming portion of my game and I’ve hit a wall trying to deal with moving platforms. I want the player to be able to jump onto a moving platform and then move with the platform. Right now the platform just slips right out from under the player as it moves in XZ, and the player falls right through if moving in Y.
I’m guessing I can solve my problem with some OnControllerColliderHit mechanism having the player snap to the contact.point, but I’m completely out of my depth trying to get it to work (and I mean completely, I can’t get anything to trigger with this function).
PS - Is it be possible to use any collider shape for CharacterController instead of the default capsule?
PPS - I forgot to ask if there could be a better description of how “motion” works in CharacterController.Move. I got it to finally work properly , and it might be obvious to more seasoned programmers, but I’m still scratching my head over how to use it any other way than what is used in the FPSWalker example…transform.TransformDirection * Time.deltaTime.
There seems to be a bug in the Controller hit callbacks, where it sometimes doesn’t get called.
So instead i made you a package that is using another trigger as the child of the first person controller to track collision with platforms.
See the attached package, it contains two scripts and an example scene.
You might want to change the OnTriggerEnter/Exit functions to filter what objects may be platforms.
A couple of notes:
Make moving platforms move at fixed framerate. If you are using animations there is a checkbox called Play fixed framerate, which you should check.
Regarding your questions:
OnControllerColliderHit seems buggy at the moment. Sorry we didn’t catch this in the beta test. We will fix this soon.
There is no way to change to another type of collider.
The CharacterController is very specialized for first person controllers at the moment. We might support more shapes later on but that might take a while.
Basically the Move function just carries out a movement you give it.
The movement is given in world space. The Controller only constrains the carried out motion by collisions and slides on the sides of walls etc like in your standard quake 3 fps controls. And does stair stepping.
Wow! Thank you so much for that excellent Christmas present
It worked like a charm once I properly spliced it into my script. It doesn’t work for platforms moving up and down though (more precisely up) as the player still falls through, but I’m going to try a workaround where the player is placed a very small amount above the platform so the collision will still trigger as the platform moves upward.
[Edit] I just noticed that if you keep the player continuously moving on the platform it will not fall through. Weird, I can’t figure out what is triggering differently to make this happen, but if I can I don’t have to do my cheesy workaround
Hmm. Could be that your script continously tries switching between being airborne and not. Or maybe you have set the min Move distance in the controller to something else than zero.
Hi Joachim, could you repost the file as a .zip, the current download doesn’t seem to be understood correctly by Safari / Firefox / OSX10.4. The downloaded file opens up 3 folders without meaningful content and ‘random’ filenames. (I remember reading a post somewhere else on the forum with a question why .zip was used since the unitypackage was essentialy the same, so maybe i’m missing something?)
thanks in advance
PS since this is my first post as a new user, just wanted to say I’m very impressed by Unity and hope to use it as a prototype / storytelling tool. I’ve always worked in Macromedia Director so I’m trying to learn C# asap and the topic of this thread was exactly the problem I was trying to solve in my first scene.
Hmm… From safari, try right-clicking, select Download Linked File.
This will give you ‘fps_platforms_181.unitypackage.txt’. Get Info on the file, and in the ‘Name and Extension’ remove the .txt (so it ends in UnityPackage).
The from Unity, do an Assets->Import Package to get it into your current project.
I’m trying that exemple and it doesn’t seem to work anymore. The character is just falling, the platforms are moving, but the character isn’t.
Is it me or what ? (I’m using pro 1.62f1)
For all interested, I just found that kicking up characterController.stepOffset fixes that. That obviously has side effects, though, and I haven’t gotten a solution going quite yet.
Wow, what an old thread! It’s surprising to me that no one has come up with a rock solid solution yet to 3rd person characters interacting with moving up/down platforms. I thought Dacloo’s parenting solution was pretty good, but he mentioned some weird transformation skewing. A little bird told me that there’s a 3D platformer tutrorial in the works… hopefully this issue will be addressed there?
Here’s a thought: Create a trigger collider that goes just above the platform. Anytime an object with either a rigidbody or charactercontroller overlaps it, move it by the same amount that the platform is moving that frame.
Here’s one. First, add these vars to your character script:
private var activePlatform : Transform;
private var activeLocalPlatformPoint : Vector3;
private var activeGlobalPlatformPoint : Vector3;
private var lastPlatformVelocity : Vector3;
Then add this code right before you Move() on your character controller inside of Update():
Then add this OnControllerColliderHit function, that will get called on a collision when Move() was called:
function OnControllerColliderHit (hit : ControllerColliderHit)
{
if (hit.moveDirection.y > 0.01)
return;
// Make sure we are really standing on a straight platform
// Not on the underside of one and not falling down from it either!
if (hit.moveDirection.y < -0.9 hit.normal.y > 0.9)
{
activePlatform = hit.collider.transform;
}
}
(Or if you already have OnControllerColliderHit, add the code within to the end of it, or whatever makes sense for the situation.
That should be it, in theory. This will get explained better in a different venue in the future.
Thanks Jon… but again this wont work… as i am using physics to control my object… force and velocity, so I cant see how inot nicely. Will totally disturbe my objects movement.
Ahh, well the topic has been about the CharacterController, not Rigidbodies, so I only assumed. I haven’t done moving platforms for rigidbody characters, but I’m sure it’s doable, and I’d guess more straightforward than CharacterController.
i solved it that way too, but anyway i dont know if it works on low fps machines … anyone has better solutions for that problem?
i also tried to put a rigidbody on the platforms and take oncollisionenter, but my player just has an charactercontroller and it doesnt work at all.
the other thing is that theres a force added to the platform.rigidbody on collision = very bad.
If you use rigidbodies this is as simple as making the platform be a kinematic rigidbody and turning the animation component’s animate physics flag on. If you move it from code you just need to use rigidbody.MovePosition and rigidbody.MoveRotation from FixedUpdate instead of transform.position. This way the kinematic rigidbody will apply friction and velocity to rigidbodies sitting on top of it.
If you are using the character controller, the third person tutorial has a script that handles platforms perfectly fine.