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 /
  • Help Room /
avatar image
0
Question by ameer469 · Mar 04, 2016 at 09:44 PM · jumpjumpinginfinite

How to stop my character from infinite jumping?

I am a new learner and i know almost nothing much about C# scripting,i have been following up with videos to make a 2D game but the only problem i faced so far is this,i cant stop my character from infinite jumping,how can i make it jump only when it touches the ground? here is my script i hope you can edit somehow that will get fixed

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
 
     //Floats
     public float maxspeed = 3;
     public float speed = 50f;
     public float jumpPower = 150f;
 
     //Booleans  
     public bool grounded;
     
 
     //Stats
     public int curHealth;
     public int maxHealth = 5;
 
 
     //References
     private Rigidbody2D rb2d;
     private Animator anim;
 
     void Start()
 
 
     {
         rb2d = gameObject.GetComponent<Rigidbody2D>();
         anim = gameObject.GetComponent<Animator>();
 
         curHealth = maxHealth;
     }
 
 
     void Update()
     {
 
         anim.SetBool("Grounded",grounded);
         anim.SetFloat("Speed", Mathf.Abs(rb2d.velocity.x));
 
         if (Input.GetAxis("Horizontal") < -0.1f)
         {
             transform.localScale = new Vector3(-1, 1, 1);
         }
 
         if (Input.GetAxis("Horizontal") > 0.1f)
         {
             transform.localScale = new Vector3(1, 1, 1);
         }
 
         if (Input.GetButtonDown("Jump") && grounded)
         {
                 rb2d.AddForce(Vector2.up * jumpPower);
                
 
         }
 
         if (curHealth > maxHealth)
         {
             curHealth = maxHealth;
         }
 
         if(curHealth <= 0)
         {
             Die();
 
         }
 
     }
 
     void FixedUpdate()
     {
         Vector3 easeVelocity = rb2d.velocity;
         easeVelocity.y = rb2d.velocity.y;
         easeVelocity.z = 0.0f;
         easeVelocity.x *= 0.75f;
 
         float h = Input.GetAxis("Horizontal");
 
         //Fake friction / Easing the x speed of our player
         if(grounded)
         {
 
             rb2d.velocity = easeVelocity;
 
         }
 
         //Moving the player
         rb2d.AddForce((Vector2.right * speed) * h);
 
         //Limiting the speed of the player
         if(rb2d.velocity.x >maxspeed)
         {
             rb2d.velocity = new Vector2(maxspeed, rb2d.velocity.y);
         }
 
         if(rb2d.velocity.x < -maxspeed)
         {
             rb2d.velocity = new Vector2(-maxspeed, rb2d.velocity.y);
         }
     }
 
     void Die()
     {
         //Restart
         Application.LoadLevel(Application.loadedLevel);
 
     }
 
     public void Damage (int Damage)
     {
 
         curHealth -= Damage;
         gameObject.GetComponent<Animation>().Play("Player_RedFlash");
     }
 
     public IEnumerator Knockback(float knockDur, float knockbackPwr, Vector3 knockbackDir) {
 
         float timer = 0;
         while( knockDur > timer)
         {
             timer+=Time.deltaTime;
 
             rb2d.AddForce(new Vector3(knockbackDir.x * -100, knockbackDir.y * knockbackPwr, transform.position.z));
 
         }
 
         yield return 0;
 
     }
 
 
 }




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
Best Answer

Answer by Lukamyte · Mar 05, 2016 at 12:54 AM

Hello Ameer.

 if(Physics2D.Linecast(transform.position, transform.position - Vector2(0, -1), 1 << LayerMask.NameToLayer("Terrain"))){
    grounded = true;
 }else if(Physics2D.Linecast(transform.position, transform.position - Vector2(0.33, -1), 1 << LayerMask.NameToLayer("Terrain"))){
    grounded = true;
 }else if(Physics2D.Linecast(transform.position, transform.position - Vector2(-0.33, -1), 1 << LayerMask.NameToLayer("Terrain"))){
    grounded = true;
 }else{
    grounded = false;
 }


This should work for you. Explanation of what it does: Each of these IF statements casts a small line below the player to check and see if there is anything below it. If there IS terrain below it, it will respond as being "grounded". http://docs.unity3d.com/ScriptReference/30_search.html?q=physics2D.linecast is the documentation for this statement.

The 1 << LayerMask.NameToLayer("Terrain") Makes sure that the line only returns true if it's colliding with the ground, and not the player. You will need to make a new layer in the Editor and assign it to the terrain to make this work. The name of the layer will need to be the same as it is in the code.

Final thing: I put this linecast 3 times so that even if the player is not standing directly over the ground, like if there was an edge, it would still allow him to jump.

I hope this helps. Sorry if I accidentally wrote this in JavaScript. ; )

Comment
Add comment · Show 3 · 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 ameer469 · Mar 05, 2016 at 08:09 AM 0
Share

i managed to get rid of that error by changing 'LineCast' to 'Linecast' and now i get several errors in Vector2,so im pretty much stuck right now

avatar image ameer469 · Mar 05, 2016 at 08:28 AM 0
Share

i managed to get rid of the error before but now im getting few error with Vector2 and i still dont know why

avatar image Lukamyte ameer469 · Mar 05, 2016 at 03:16 PM 0
Share

Turns out I had one more right parentheses than was needed at the end of those if statements. I changed it in the answer to fit. So, if you just copy the code above again, it has the revisions you stated you made and the ones I've made too. I hope this is the only issue you were having.

avatar image
0

Answer by painproductions · Mar 05, 2016 at 12:05 PM

This should work Finding the Ground

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

49 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

Related Questions

Player Is infinite jumping 1 Answer

how to stop infinite jumping in air? 0 Answers

Hello! I have infinite jump problem, please help me! 0 Answers

Jump Force/Velocity is Wrong and Strange. Help 1 Answer

I am slowly falling down with the jump animation 4 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