Ok, I have been on this question for a few hours and I found a way :D. It’s not a beautiful solution but it works pretty well.
I asked myself : If you cannot bounce with a CharacterController, why not make him jump again?
So the idea is to place a thin collider over the enemy (a cube is better than a plane). You check the “isTrigger” option and uncheck the “meshRenderer”. Then, you script this invisible cube to always be above the enemy like this:
transform.position = new Vector3(
enemy.transform.position.x,
enemy.transform.position.y +0.7f,
enemy.transform.position.z);
Then what you want is to jump again as soon as the player lands on the enemy’s head. The problem is that you cannot write : jump! You can also not fake an input like the space bar. But you can tell the CharacterControllerMotor to have his boolean variable inputJump to ‘true’. So, if you enter the trigger of the invisible cube, just script:
CharacterMotor.inputJump = true;
The problem here is that the FPSInputController is always checking for the space bar to be pushed. If you open the JS script, you can see, at the bottom:
motor.inputJump = Input.GetButton("Jump");
So the solution is to comment that line of code in your FPSInputController, then to add a space bar event in your update function. Here is the complete code you can place on the invisible cube (enemy is the enemy, player is the player :D):
GameObject enemy;
CharacterMotor motor;
void Awake () {
enemy = GameObject.FindWithTag("Enemy");
motor = GameObject.FindWithTag("Player").GetComponent<CharacterMotor>();
}
void Update () {
transform.position = new Vector3(
enemy.transform.position.x,
enemy.transform.position.y +0.7f,
enemy.transform.position.z);
if(Input.GetKeyDown("space")){
motor.inputJump = true;
}
if(Input.GetKeyUp("space")){
motor.inputJump = false;
}
}
void OnTriggerEnter(Collider col) {
motor.inputJump = true;
}
void OnTriggerExit(Collider col) {
motor.inputJump = false;
}
Here it is, it works! Don’t forget to comment the “jump code line” in the FPSInputController. You can also change the jump speed in the motor, etc…
Hope it helps!
LeMoine