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 clarke3018 · Dec 23, 2013 at 01:10 AM · 2dcharacterjumping

cant jump in 2d?

i have just watched the 2D Character Controllers video tutorial

and i haven't had problems working through it until it got to the jump part, my character doesn't jump when i click space, but the "vspeed" is working because if i walk off the edge of my ground the "vspeed" changes, showing my animation. i don't know if you know what im talking about so ill just show you the script and hopefully someone can tell me the problem.

**using UnityEngine; using System.Collections; public class charactercontrol : MonoBehaviour {

 public float maxspeed = 2f;
 bool facingright = true;
 
 Animator anim;
 
 bool grounded = false;
 public Transform groundcheck;
 float groundRadius = 0.2f;
 public LayerMask whatisground;
 public float jumpforce = 700f;
 
 // Use this for initialization
 void Start () {
     anim = GetComponent<Animator> ();
 }
 
 // Update is called once per frame
 void FixedUpdate () 
 {
     
     grounded = Physics2D.OverlapCircle (groundcheck.position, groundRadius, whatisground);
     anim.SetBool ("Ground", grounded); 
     anim.SetFloat ("vspeed", rigidbody2D.velocity.y);
     
     
     float move = Input.GetAxis ("Horizontal");
     
     anim.SetFloat("speed", Mathf.Abs(move));
     
     rigidbody2D.velocity = new Vector2 (move * maxspeed, rigidbody2D.velocity.y);
     
     if (move > 0 &&!facingright)
         Flip();
     else if (move < 0 && facingright)
         Flip ();
 }
 void update()
 {
     //float jump = Input.GetAxis ("Vertical");
     //anim.SetFloat ("jumpforce", Mathf.Abs (jump));
     if (grounded && Input.GetKeyDown(KeyCode.Space))
     {
         anim.SetBool("Ground", false);
         rigidbody2D.AddForce(new Vector2 (0, jumpforce));
     }
 }
 
 void Flip()
 {
     facingright = !facingright;
     Vector3 theScale = transform.localScale;
     theScale.x *= -1;
     transform.localScale = theScale;
     
 }

}**

thanks.

Comment
Add comment · Show 6
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 Santosh Patil · Dec 23, 2013 at 06:10 AM 0
Share

Have you checked the value of "grounded"

if it is false every time then problem would in line code.

 grounded = Physics2D.OverlapCircle (groundcheck.position, groundRadius, whatisground);

you can try another way to check character is grounded or not as follow

 grounded = Physics2D.Linecast(transform.position,groundcheck.position,  1 << Layer$$anonymous$$ask.NameToLayer("NameOfGroundLayer"));
avatar image clarke3018 · Dec 23, 2013 at 10:59 AM 0
Share

once i changed it to that it no longer recognized the ground, with the one i have it recognizes the ground and when i fall off the edge, the parameter gets unticked and i go into my falling animation. i think the problem is when i press space nothing happens, no force is pushed up.

avatar image KellyThomas · Dec 23, 2013 at 11:10 AM 0
Share

What is value is jumpForce set to in the inspector?

avatar image clarke3018 · Dec 23, 2013 at 12:29 PM 0
Share

it is also set to 700 in the inspector

avatar image KellyThomas · Dec 23, 2013 at 12:51 PM 0
Share

Please try replacing your jump code with this:

 if (grounded && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
 {
     Debug.Log("Trying to jump");
     anim.SetBool("Ground", false);
     rigidbody2D.AddForce(new Vector2 (0, jumpforce));
 }


If when you run this code the message is printed to console, then check your gravity/mass values, otherwise check your input and ground check.

Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by KellyThomas · Dec 23, 2013 at 01:26 PM

Sorry I was working from my phone earlier and could only see small areas of your code at a time.

Now I am at a PC I have noticed:

  1. Your Update function should have a leading capital i.e. "`void update()`" should be "`void Update()`"

  2. Your update logic is spilt between FixedUpdate and Update? Is there a reason for this split?

Correcting the spelling of "`Update`" should fix the immediate problem.

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

Answer by clarke3018 · Dec 23, 2013 at 02:24 PM

oh dear it was that simple, I'm sorry for wasting your time hahaa all i had to do was put a capital letter on my void Update(), and its split so that the jump key doesnt get missed.

thankyou very much.

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

Answer by Lune · Mar 21, 2014 at 04:23 AM

i have found that if your sprite has an Animator component, you have to deactivate "apply root motion" because that causes conflict with the 2d physics

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

Answer by Dimension99 · Aug 30, 2016 at 10:05 AM

,I really don't know how to thank you very very very dear Kelly, I was stuck in this like more than a week and I really didn't know what to do to make it jump. you know, I'm kinda very new to unity and it didn't give me any error either. so I couldn't find the problem (Capitalizing U in "Update"). You did me a very very very great favor. Thank you so much. GOOOOOOOD LUCK @username

From: Aref Bikaran/ Kabul Polytechnic University Afghanistan

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

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

21 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

Related Questions

Flat 2d image on 3d character. 0 Answers

when 2d character hits wall it shakes/bounces 1 Answer

2D Character creator ,how can i save current clothes (changes) 1 Answer

Ui sidescroller 2d touch ? 0 Answers

Im trying to make it so it limits jumps in my 2d game. But when I play it, It only jumps once then your not able to jump again. Anyhelp? I need a answer fast because this is for the blackthornprod game jam 2 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