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 TempestInATeacup · May 21, 2015 at 08:26 PM · c#movementcharactercontrollerjump

Jump with Character Controller

I'm sorry if this is a stupid question, but I'm trying to figure out how to make a character jump using the character controller. I've been looking through the documentation, googling, looking through this forum, and watching youtube videos for about 3 hours now, and none of the stuff I'm finding is helping. I have a cube in my game that can walk around by rotating and moving forward, but how on earth do I make it jump with space? I've copy pasted my entire code below; please keep in mind that I am an absolute beginner to Unity and to coding in general. I got most of this from a youtube video, so a lot of it looks sloppy to me and I'm not entirely sure what all of it does... I just don't know what to put for when jump is true.

using UnityEngine; using System.Collections;

[RequireComponent(typeof(CharacterController))]

public class PlayerController : MonoBehaviour {

 public float moveSpeed;
 public float rotateSpeed;
 public float jumpSpeed;
 CharacterController cc;

 private bool jump;

 void Start ()
 {
     cc = GetComponent<CharacterController> ();
 }

 void Update()
 {
     Vector3 forward = Input.GetAxis ("Vertical") * transform.TransformDirection (Vector3.forward)*moveSpeed;
     transform.Rotate (new Vector3(0.0f, (Input.GetAxis ("Horizontal")*rotateSpeed*Time.deltaTime), 0.0f));

     cc.Move (forward * Time.deltaTime);
     cc.SimpleMove (Physics.gravity*Time.deltaTime);

     if (Input.GetKeyDown (KeyCode.Space))
     {
         jump = true;
     }

     if (jump) 
     {

     }

 }

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Gixtox · May 21, 2015 at 08:59 PM

So if I am correct it looks like you check to see if the Space bar is pressed and then do nothing with that. You say if space is pressed then jump is true. But then you say if jump is true, nothing. Add something into your jump if statement like a force.

http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

Add a force in the right direction and your cube should jump. If you want to go a step further you can cut out the jump = true; and just put an Addforce in your first if statement.

Comment
Add comment · Show 5 · 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 TempestInATeacup · May 22, 2015 at 01:42 PM 0
Share

Right but like I said I don't know what to put in the if (jump), I've just copied this mostly from videos and tutorials. I'm not using a Rigidbody, I'm using a character controller, and I might be wrong but I don't think you can use Addforce with a character controller

avatar image Gixtox · May 22, 2015 at 02:31 PM 0
Share

So I looked at the character controller script reference here.

http://docs.unity3d.com/ScriptReference/CharacterController.$$anonymous$$ove.html

So I've never used a character controller but here is how I'm understanding it. If you look at their code and your code, they use controller.$$anonymous$$ove while you use cc.$$anonymous$$ove. So all you need to do is put that into your if last if statement. It should be something like

cc.$$anonymous$$ove(0,jumpSpeed*Time.deltaTime,0);

avatar image TempestInATeacup · May 22, 2015 at 02:43 PM 0
Share

I've made a few changes, and now it jumps when you press space so I think I'm getting somewhere BUT it keeps jumping. it just goes up and down forever. This is what I have:

public class PlayerController : $$anonymous$$onoBehaviour {

 public float moveSpeed;
 public float rotateSpeed;
 public float jumpSpeed;
 CharacterController cc;

 private bool jump;

 void Start ()
 {
     cc = GetComponent<CharacterController> ();
 }

 void Update()
 {
     Vector3 forward = Input.GetAxis ("Vertical") * transform.TransformDirection (Vector3.forward)*moveSpeed;
     transform.Rotate (new Vector3(0.0f, (Input.GetAxis ("Horizontal")*rotateSpeed*Time.deltaTime), 0.0f));

     cc.$$anonymous$$ove (forward * Time.deltaTime);
     cc.Simple$$anonymous$$ove (Physics.gravity*Time.deltaTime);

     if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space))
     {
         jump = true;
     }

     if(jump) 
     {
         Vector3 jumpUp = transform.TransformDirection(Vector3.up)*jumpSpeed;
         cc.$$anonymous$$ove (jumpUp * Time.deltaTime);
     }

 }

}

avatar image Gixtox · May 22, 2015 at 02:47 PM 0
Share

You never set jump back to false. Add

jump = false;

to the end of the if statement.

avatar image TempestInATeacup · May 22, 2015 at 02:50 PM 0
Share

Thank you! now I just have to make it look less clunky.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

[C#] Jump on slopes 1 Answer

Jump and move (CharacterController.velocity) C# 0 Answers

Making a bubble level (not a game but work tool) 1 Answer

My Player can't jump - coding 1 Answer

Need helping getting a character controller to 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