hello. i am trying to make a model to move up and down with the unity animation and for some reason when my character will not collide.
Hi.
Please could you explain this a little bit better? I personally cant quite follow what you mean…
Cheers
AC
sorry i was in a hurry. i am making a object with a mesh or box collider move up and down and i need the character to stand on it. but when it starts moving he falls through.
Theres been a few issues with people implementing this in the past.
http://forum.unity3d.com/viewtopic.php?t=1092
I havent come up against this problem yet so I cant really help you. There is a package at this link that may help, and lots of posts with code etc.
Hopefully thats a help. Further forum searching might yield more success.
AC
thank you for the link. but did not help.
I’m not sure if this is the best solution, but it works for me: I place a trigger (parented to the platform) directly on top of the platform and have a parenting/unparenting script on the trigger. The First Person Controller steps onto the platform, becomes parented to it, stays with it as it rises and falls or moves sideways, etc… Then, when the FPC steps off the platform, he’s unparented from it and free to go on his merry way. Here’s the code:
function OnTriggerEnter () {
var GO = GameObject.Find("MovingPlatform");//whatever the name of your moving platform is
var GO1 = GameObject.Find("First Person Controller");
GO1.transform.parent = GO.transform;
}
function OnTriggerExit () {
var GO = GameObject.Find("MovingPlatform");
var GO1 = GameObject.Find("First Person Controller");
GO1.transform.parent = null;
}
Note: Make sure the skin of your First Person Controller capsule is thick enough (you have to experiment with that), or it will fall through. In my platforming code I also used triggers to get the animation of the platform started, but I didn’t include that here. Good luck!
Edit: If you’re not using a first person controller, it should still work with an animated third person character. Just change the character name in the variable declaration.
Thanks man that script helped me out m8.
C# pseudo code, convert to javascript if needed
this should work almost flawlessly for a CharacterController on a rigidbody moving platform
Transform activePlatform;
Vector3 activeLocalPlatformPoint;
Vector3 activeGlobalPlatformPoint;
Vector3 lastPlatformVelocity;
CharacterController controller;
void OnControllerColliderHit (ControllerColliderHit hit) {
if (hit.moveDirection.y > 0.01f)
return;
if (hit.moveDirection.y < -0.9f hit.normal.y > 0.9f) {
activePlatform = hit.collider.transform;
}
}
void Update () {
// relative movement
if (activePlatform != null) {
Vector3 newGlobalPlatformPoint =
activePlatform.TransformPoint(activeLocalPlatformPoint);
Vector3 moveDistance =
(newGlobalPlatformPoint - activeGlobalPlatformPoint);
transform.position = transform.position + moveDistance;
lastPlatformVelocity =
(newGlobalPlatformPoint - activeGlobalPlatformPoint) / Time.deltaTime;
} else {
lastPlatformVelocity = Vector3.zero;
}
activePlatform = null;
// character moving code
Vector3 velocity = Physics.gravity * Time.deltaTime;
controller.Move(velocity);
// localize movement
if (activePlatform != null) {
activeGlobalPlatformPoint = transform.position;
activeLocalPlatformPoint =
activePlatform.InverseTransformPoint (transform.position);
}
}
This scripts works for me in an elevator setup when going up but I still get a falling sensation when I go down. Is there someway to work around that?
Thanks.
Parenting is not a solution to physics-based motion. Physics is best treated in the same frame of reference.
Something similar to this (took this snippet from a backup, not reliable):
void Update () {
Vector3 velocity = Physics.gravity * Time.deltaTime;
if (platform != null) {
velocity = platform.velocity * Time.deltaTime;
}
controller.Move(velocity);
}
public override void OnControllerColliderHit (ControllerColliderHit hit) {
if (hit.moveDirection.y > 0.01f)
return;
if (hit.moveDirection.y < -0.9f hit.normal.y > 0.9f) {
platform = hit.collider.rigidbody;
}
}
There are a few things incomplete with this code, but basically, just take the velocity of the rigidbody he is standing on (or the delta position of a non dynamic object) and add it to your character. Also note this chunk of code is in Update, use Fixed instead. Anyway, I ended scrapping the CharacterController and rolled my own implementation based on rigidbody w/ capsule collider. This is not recommended for every situation, though.