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
0
Question by SBull · Jun 24, 2013 at 07:34 PM · charactercontrollerjumpplatformisgrounded

How do you make a characterController jump automatically when it reaches the edge of a platform?

I'm making a 2D game where several characterControllers (created as prefabs) move by themselves along platforms. I'm trying to make it so that they jump automatically when they reach the edge of a platform, but all of the jumping scripts that I've looked at so far don't seem to work. I have it so that the game recognizes when the character is touching the ground and begins moving them automatically, but that's it so far. Could someone just give me a basic idea of the lines that I will need to use to make this happen? Thanks for any help.

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 Em3rgency · Jun 24, 2013 at 07:37 PM 0
Share

I'm thinking invisible objects at the edges? When you detect collision with one of them, you order a jump.

avatar image DerTeufel · Jun 24, 2013 at 07:41 PM 0
Share

you can add colliders at the edges of your plattform. Raycast down from your player. If hit edge, jump automatically!

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by fafase · Jun 24, 2013 at 07:41 PM

2 solutions I can think of:

You could place a trigger plane at the end of each platform and when entering the plane, it jumps. Since you are in 2D unity:

 public float speed = 6.0F;
 public float jumpSpeed = 8.0F;
 public float gravity = 20.0F;
 private Vector3 moveDirection = Vector3.zero;
 CharacterController controller;
 bool onceJump = false;
 void Start(){
    controller = GetComponent<CharacterController>();
 }
 void Update() {
     if(controller.isGrounded){
        moveDirection = Vector3.zero;
    moveDirection.x = 1;
        moveDirection.x = speed;
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;
     }
     moveDirection.y -= gravity * Time.deltaTime;
     controller.Move(moveDirection * Time.deltaTime);
 }
 void OnTriggerEnter(Collider col){
    if(col.transform.tag == "Player")moveDirection.y = jumpSpeed;
 }

Other way is to use the isGrounded variable. As soone as your guy will start falling, bam it jumps.

 public float speed = 6.0F;
 public float jumpSpeed = 8.0F;
 public float gravity = 20.0F;
 private Vector3 moveDirection = Vector3.zero;
 CharacterController controller;
 bool onceJump = false;
 void Start(){
     controller = GetComponent<CharacterController>();
 }
 void Update() {
     
     if (controller.isGrounded) {
         moveDirection = Vector3.zero;
     moveDirection.x = 1;
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection *= speed;   
         onceJump = true;
     }
     else if (!controller.isGrounded && onceJump){
             moveDirection.y = jumpSpeed;
         onceJump = false;
     }
     moveDirection.y -= gravity * Time.deltaTime;
     controller.Move(moveDirection * Time.deltaTime);
 }


Working and tested.

So if you are grounded, you walk, if you are not grounded, you jump but only once. If you are not grounded but you cannot jump, you are going down until you are grounded.

Comment
Add comment · Show 10 · 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 SBull · Jun 25, 2013 at 06:41 PM 0
Share

Thanks to those who responded. I might not have been clear in my question. I understand the idea behind when to make something jump (I've been going with the !isGrounded then jump method) to try and make it jump as soon as it starts falling, but I'm still pretty new to scripting and can't figure out how to simply make a CharacterController jump. I can do it with a rigidbody using rigidbody.AddForce(transform.up/right), but I'm confused at how to achieve a similar effect using a CharacterController. I think:

moveDirection.x = speed;

moveDirection = transform.TransformDirection(moveDirection);

moveDirection *= speed;

is what I am looking for, but unfortunately I am getting errors when I put this script in. Would you be willing to break some of this down to explain what variables and commands I need for this?

avatar image fafase · Jun 26, 2013 at 10:10 AM 0
Share

I updated the answer

avatar image SBull · Jun 26, 2013 at 05:08 PM 0
Share

Hmmm. I wonder if you are using a different version than I am, because I put the exact code that you listed and it's still giving me lots of errors. I should note that I am using Javascript.

avatar image robertbu · Jun 26, 2013 at 05:09 PM 0
Share

The code above is C#. You would need to put this code in C# scripts to get them to compile in Unity.

avatar image Em3rgency · Jun 26, 2013 at 05:25 PM 0
Share

As an afterthought, next time you ask something, include either the js or c# in the tags :)

Show more comments

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

18 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Why does my characterController code not apply gravity correctly? 0 Answers

Addforce to create a gliding effect? 2 Answers

Translation of an object 2 Answers

Why my character wont jump??? 1 Answer


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