Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 unity_evd1dv8LgVq4jA · Dec 05, 2017 at 03:16 PM · animatorjumpingcharacter controllercharacter movement

how to make 2d character jump?

Hello! I'm currently working on a game as the main animator and character coder. And I have a issue, I currently have the animations and code for the character to move left, right and to be idle. But I am also trying to implement the character to jump, each tutorial I get confused because some things that they type are not working in mine ((guessing it's outdated)) I made the bool for ground and a game object called "GroundCH" I put it at the characters feet but that's as far as I could go without getting completely confused on these tutorials

here's my code for the character to just move around

 public float maxSpeed = 8f;
 bool left = true;
 Animator a;


 void Start () {

     a = GetComponent<Animator> ();
     
 }

 void FixedUpdate () {

     float move = Input.GetAxis ("Horizontal");
     a.SetFloat ("Speed", Mathf.Abs (move));
     GetComponent<Rigidbody2D> ().velocity = new Vector2 (move * maxSpeed, GetComponent<Rigidbody2D> ().velocity.y);

     if (Input.GetKey (KeyCode.LeftArrow)) {
         transform.position += Vector3.left * maxSpeed * Time.deltaTime;
     
     }
     else if (Input.GetKey (KeyCode.RightArrow)) {
         transform.position += Vector3.right * maxSpeed * Time.deltaTime;

     }
     

     if (move > 0 && left)
         Flip ();
     else {
         if (move < 0 && !left)
             Flip ();
         }
 
     
     }

 void Flip()
 {
     left = !left;
     Vector3 scale = transform.localScale;
     scale.x *= -1;
     transform.localScale = scale;
 }
     
     

}

Comment
Add comment · Show 3
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 megabrobro · Dec 05, 2017 at 03:32 PM 0
Share

I dont see any code that will make the character jump here. There is more than one way to make it jump, some more complicated that others. The most simple I can think of is: rb = GetComponent(); float jumpHeight = 5f; if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)){ rb.transform.position += new Vector2(rb.transform.position.x, rb.transform.position.y += jumpHeight);

avatar image megabrobro · Dec 05, 2017 at 03:34 PM 0
Share

Of course, this will allow the user to keep jumping every time he presses Space, which will be poor gameplay as he can just jump through the sky without worry. $$anonymous$$ost of the time you check if he collides with floor. $$anonymous$$y favourite way in 3d is to use a Raycast from the centre of the sprite, pointing downwards, called IsGrounded and it returns true if the Raycast hits anything. Here is a link, I'm fairly sure it would work in 2d too: https://answers.unity.com/questions/196381/how-do-i-check-if-my-rigidbody-player-is-grounded.html

avatar image megabrobro · Dec 05, 2017 at 03:36 PM 0
Share

a more simple way than checking collision with ground would be: if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && rb.velocity.y

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Can't make my character to jump ?? 0 Answers

Jump while running not faster than normal jump. Any help with the code? 1 Answer

Shift BHOP in Character Controller 0 Answers

My Character controller keeps jumping infinitely 2 Answers

[SOLVED] 2D Character Controller gains velocity when colliding with a corner 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