Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by darienurse · Nov 29, 2012 at 09:23 PM · animationcharacterjumpcharacter controller

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image greatestprez · Nov 29, 2012 at 09:46 PM 0
Share

you'd have to edit the source code.

avatar image darienurse · Nov 30, 2012 at 01:50 AM 0
Share

I know that but what aspect of the source code?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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
     }
   }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image darienurse · Dec 02, 2012 at 04:23 PM 0
Share

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?

avatar image aldonaletto · Dec 03, 2012 at 02:40 AM 0
Share

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.

avatar image darienurse · Dec 04, 2012 at 12:27 AM 0
Share

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.

avatar image aldonaletto · Dec 04, 2012 at 02:54 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges