- Home /
Minor Addition to Jumping using CharaterController
I'm using a model based on the Character Controller asset. I know that the character can jump using space bar but I want to modify the character so that it will jump upon colliding with an object. How can i change the Character Controller scripts to implement this action?
Answer by aldonaletto · Nov 30, 2012 at 03:25 AM
Modifying the First Person Controller scripts isn't an easy task, and may screw things up. It would be better to add a new script to the character, like below:
 var motor: CharacterMotor; // reference to the FPC script
 
 function Start () {
     motor = GetComponent(CharacterMotor);
 }
 
 private var jumpTime: float = 0;
 
 function OnControllerColliderHit(hit: ControllerColliderHit){
     if (hit.normal.y < 0.707){ // if collision normal < 45 degrees...
         // check the collision direction:
         var dir = hit.point - transform.position;
         dir.y = 0; // keep only the horizontal direction
         // if collision in front side...
         if (Vector3.Dot(dir, transform.forward) > 0){
             jumpTime = Time.time + 0.1; // send a 0.1s jump command
         }
     }
 }
 
 function LateUpdate () {
     // send jump command during the time specified:
     motor.inputJump |= jumpTime > Time.time;
 }
The CharacterMotor script jumps when its variable inputJump gets true, what happens at LateUpdate to avoid problems with script execution order
EDITED:
The code above works for the First Person Controller. If you're using the Third Person Controller prefab, forget about an external script - the only solution is to edit its control script. But you're a lucky guy, the change is simple: find the function OnControllerColliderHit in the script ThirdPersonController.js (in the folder Standard Assets/Character Controllers/Sources/Scripts) and replace it with the following code:
 function OnControllerColliderHit(hit: ControllerColliderHit){
   if (hit.normal.y < 0.707){
     var dir = hit.point - transform.position;
     dir.y = 0;
     if (Vector3.Dot(dir, transform.forward) > 0){
       lastJumpButtonTime = Time.time; // this causes a jump
     }
   }
 }
Thank You so much! This has gotten me on the right track but this code only causes the character to hop without animation. How do I implement the same jumping animation the happens when you press spacebar?
I thought you were using the First Person Controller. For the 3rd Person Controller, you must modify the script ThirdPersonController.js (is in the folder Standard Assets/Character Controllers/Sources/Scripts). Take a look at my edited answer.
Thank you! this is exactly what I needed. I dont know why that wasnt immediately obvious. I feel like you should be able to make a character jump by simply calling characterState == CharacterState.Jumping and then update. And Im still not sure why a Charater$$anonymous$$otor is necessary; I guess I just have to study the code some more.
The logic used in the ThirdPersonController script is very complex, but you can learn how it start a jump by searching for "Jump", the name of the jump button - it's read in two different places, but only in Update it does something useful.
 Character$$anonymous$$otor isn't used in the 3rd Person Controller, only the script ThirdPersonController. 
Your answer
 
 
             Follow this Question
Related Questions
How do I get my jump animation to work? 1 Answer
Best way to go about believable 2 Character Grab/Lift animation? 1 Answer
Need help addind a attack and jump script 0 Answers
Changing the jump key in a premade code 4 Answers
How can I use a joystick for a mobile app to control specific limbs of a character? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                