Hey Norbo! I'm wondering if I can get my character controller to behave temporarily like a normal physics object, with normal material interactions, and then switch back
to the standard character controller behavior with zero friction and bounciness. I just tried adding a delegate instance to the character's DetectingInitialCollision event handler, and then swapped the methods of the delegate from the old one (RemoveFriction) to one that simply does nothing... But because of the way the initial collision detected event works, it's not going to work when the collision has already happened and then I want to switch from a frictionless state to one with friction on a collision pair which already exists.
So, my question is, should I call UpdateMaterialProperties for each pair the character controller's body is involved in when I want to switch behavior? Or should I simply change the implementation of the character controller to not use the DetectingInitialCollision at all, and instead, relying on the fact that MaterialInteractions are now calculated multiplicatively, simply switch the frictions and bounciness to 0 whenever I want normal character controller behavior? Basically, I guess this is a question of "which one will be less likely to break subtle things or lead to odd situations?"
Thanks a lot,
Alex
Getting Char Controller to Act Like Normal Physics Entity
Re: Getting Char Controller to Act Like Normal Physics Entit
These two are roughly equivalent. Changing the material of an entity will result in the entity calling UpdateMaterialProperties on the pairs. Both will 'work' equally well.So, my question is, should I call UpdateMaterialProperties for each pair the character controller's body is involved in when I want to switch behavior? Or should I simply change the implementation of the character controller to not use the DetectingInitialCollision at all, and instead, relying on the fact that MaterialInteractions are now calculated multiplicatively, simply switch the frictions and bounciness to 0 whenever I want normal character controller behavior? Basically, I guess this is a question of "which one will be less likely to break subtle things or lead to odd situations?"
However, both bounciness and friction will conflict with the character controller's proper movement. The VerticalMotionConstraint will fight bounces and the HorizontalMotionConstraint would be fighting against 'brakes' for the duration of the effect. You can mitigate these issues by ensuring that pairs with the support entity always have 0 friction and bounciness, though this may go against your desired behavior. (Character 'bouncing' on landing is probably best handled explicitly with special case code.)
Re: Getting Char Controller to Act Like Normal Physics Entit
Hmm... as is the way of these things, your post leads me to another, deeper question.
I'm basically wanting the character controller to behave like a normal physics entity while they are reacting to a hit, so the character can be launched into the air and bounce across the ground, etc. The main reason doing this with basic physical forces seems ideal is that most of the ai creatures in my game aren't using a character controller, so the entire attack/hit reaction system works with knocking physics entities around, but doesn't work with character controllers. Rather than abstracting a whole layer for getting the character controller hit reaction behavior to be just like the normal physics entity hit reaction behavior, I thought it would be easier and cleaner to just have the character controller temporarily act like a normal physics entity with no constraints.
To accomplish that goal, I've got these lines to modify the character controller's behavior whenever they're going into a hit reaction state:
if (AllowVerticalForce)
{
characterController.VerticalMotionConstraint.MaximumGlueForce = 0f;
characterController.VerticalMotionConstraint.SolverSettings.MinimumImpulse = 0f;
}
characterController.HorizontalMotionConstraint.SolverSettings.MinimumImpulse = 0f;
characterController.HorizontalMotionConstraint.MaximumForce = 0f;
characterController.MovementDirection = Vector2.Zero;
This seems to keep the constraints from making a difference, and frees up the character controller's physics entity to be acted on by normal forces/velocity changes. But I have no idea if this is actually a reliable solution beyond the testing I've done (where it seems to work), so to quote an SNL character*, is that bad?
Thanks Norbo
Alex
*sorry about the ad
I'm basically wanting the character controller to behave like a normal physics entity while they are reacting to a hit, so the character can be launched into the air and bounce across the ground, etc. The main reason doing this with basic physical forces seems ideal is that most of the ai creatures in my game aren't using a character controller, so the entire attack/hit reaction system works with knocking physics entities around, but doesn't work with character controllers. Rather than abstracting a whole layer for getting the character controller hit reaction behavior to be just like the normal physics entity hit reaction behavior, I thought it would be easier and cleaner to just have the character controller temporarily act like a normal physics entity with no constraints.
To accomplish that goal, I've got these lines to modify the character controller's behavior whenever they're going into a hit reaction state:
if (AllowVerticalForce)
{
characterController.VerticalMotionConstraint.MaximumGlueForce = 0f;
characterController.VerticalMotionConstraint.SolverSettings.MinimumImpulse = 0f;
}
characterController.HorizontalMotionConstraint.SolverSettings.MinimumImpulse = 0f;
characterController.HorizontalMotionConstraint.MaximumForce = 0f;
characterController.MovementDirection = Vector2.Zero;
This seems to keep the constraints from making a difference, and frees up the character controller's physics entity to be acted on by normal forces/velocity changes. But I have no idea if this is actually a reliable solution beyond the testing I've done (where it seems to work), so to quote an SNL character*, is that bad?
Thanks Norbo
Alex
*sorry about the ad
Re: Getting Char Controller to Act Like Normal Physics Entit
A slightly more direct approach would be to deactivate the constraints entirely:
As long as IsActive is set to false, they will not be involved in the solving process.

While the maximum force version has some holes (crouching/sliding/air maximum forces are separate from the standing maximum force property), setting IsActive to false should work as you expect without any issues.
(By the way, the SolverSettings.MinimumImpulse defines the threshold below which the impulse is considered small enough to contribute to the constraint's inactivity. Setting it to zero actually makes the constraint do more work since it can't early-out.)
Code: Select all
CharacterController.VerticalMotionConstraint.IsActive = false;
CharacterController.HorizontalMotionConstraint.IsActive = false;
But I have no idea if this is actually a reliable solution beyond the testing I've done (where it seems to work), so to quote an SNL character*, is that bad?

While the maximum force version has some holes (crouching/sliding/air maximum forces are separate from the standing maximum force property), setting IsActive to false should work as you expect without any issues.
(By the way, the SolverSettings.MinimumImpulse defines the threshold below which the impulse is considered small enough to contribute to the constraint's inactivity. Setting it to zero actually makes the constraint do more work since it can't early-out.)
Re: Getting Char Controller to Act Like Normal Physics Entit
Thanks Norbo! (Boy am I glad I asked). 
