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 madamepriss · Mar 22, 2015 at 11:12 PM · unity 5unity 2djumping

2d Character won't jump

I am current going through Microsoft Virtual Academy and I have come across one of their scripts which won't let me jump. I assume it is because of the changes from Unity 4 to 5(Unity updated this script for me), but I can't figure it out.

I added the statement if(Input.GetButtonDown("Fire1")) {} as it seemed to be missing from the downloaded script but I cannot figure what else could be wrong.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;

 //This script makes the character move when the screen is pressed and handles the jump
 public class CharacterFinal : MonoBehaviour
 {
 public bool jump = false;        // Condition for whether the player should jump.    
 public float jumpForce = 10.0f;        // Amount of force added when the player jumps.
 private bool grounded = false;        // Whether or not the player is grounded.
 public int movementSpeed = 10;        // The vertical speed of the movement
 private Animator anim;            // The animator that controls the characters animations

 void Awake()
 {
     anim = GetComponent<Animator>();
 }
 

 //This method is called when the character collides with a collider (could be a platform).
 void OnCollisionEnter2D(Collision2D hit)
 {
     grounded = true;

 }

 //The update method is called many times per seconds
 public void Jump()
 {
     if (Input.GetButtonDown("Fire1")) {
         // If the jump button is pressed and the player is grounded and the character is running forward then the player should jump.
         if (grounded == true) {
             jump = true;
             grounded = false;
             //We trigger the Jump animation state
             anim.SetTrigger ("Jump");
         }
     
     }

 }

 public void Slide()
 {
     if(grounded == true)                        
     {
         //slide = true;
         //We trigger the Jump animation state
         anim.SetTrigger("slide");
     }

 } 




 //Since we are using physics for movement, we use the FixedUpdate method
 void FixedUpdate ()
 {

     //if died that 
     GetComponent<Rigidbody2D>().velocity = new Vector2(movementSpeed, GetComponent<Rigidbody2D>().velocity.y );
     //else
     //moving
 
     
     // If jump is set to true we add a quick force impulse for the jump
     if(jump == true)
     {
         // Add a vertical force to the player.
         GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, jumpForce),ForceMode2D.Impulse);
         
         // We set the variable to false again to avoid adding force constantly
         jump = false;
     }
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Socrates · Mar 23, 2015 at 02:53 AM

In the code you posted, you have a function Jump() that checks if the button is pressed and sets up the jump if it is pressed. However, nowhere in the code you posted is that function called. You probably want to insert a call to Jump() just before checking the value of 'jump' on line 68.

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 gsapundz · Apr 09, 2015 at 04:30 AM

Try disabling the 'Apply Root Motion' checkbox from the Animator Component of your Character

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to prevent or reduce game graphic glitches? 0 Answers

Implement Zoom(using UI Canvas) in Unity 2d game. 0 Answers

Build Unity iOS project in jenkins 0 Answers

Cant access the Image icon from class 1 Answer

Raycasting bug when checking if player is grounded 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