Boncy on Jump Enemy heads

Hello guys, I’m doing a game, and I want to make the player jump again when it jump on a enemy. Like the image below

I made this code to do this, but I have another script with CharacterController.Move on player.

Take a look:

using UnityEngine;
using System.Collections;
	class stopTween : MonoBehaviour
	{
        public CharacterController ctrl;
        public Vector3 speed;
        public float smooth = 0.6F;
        public void OnTriggerStay(Collider hit)
        {
            if (hit.gameObject.tag == "Cube")
            {
                speed.y = transform.position.y * 2*Time.deltaTime;
                ctrl.Move(speed);            
            }
           
        }
	}

The player is not Jumping well with this code, it look like a “teleport” I need to make the player only jump upward, my another move script has an airmove that able the Player control it on air.

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

You can always choose to tween the player while he’s jumping. Just do something like.

Vector3 jumpVect = new Vector3(transform.position.x, transform.position.y * speed * 
Time.deltaTime, transform.position.z);
transform.position = jumpVect;

Then just record his Y value where the ground is and reverse the tween when he hits the apex of his jump to get him back on the ground. I would make a small kinematic ridgidbody to sit on the enemies head and then a “damage area” childed to your characters feet. This why when the players feet collide with the collision on the enemies head you will have a “death event” to key into.

I have found the built in physics model to be lacking when it comes to character locomotion. In 2D games I will still use physX if I need to do something like throw a box or etc but I tend to jumping by tweening transforms myself.