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
1
Question by braydon97 · Sep 28, 2014 at 01:42 PM · c#2djumpinginfinite runner

I only want my character to jump when touching the ground

I am new to unity and C# and I got this far. and was wondering if someone could help me with the code for this last bit.

What I have is that the character will jump and jump as soon as he touches the ground due to the

"if collider.collision.tag == "Ground"" then the code down here to make him jump.

is there a way I can make it Only jump IF he is on the ground and I hit the spacebar/Mouse

Thanks in advance here is the code!

 using UnityEngine;
 using System.Collections;
 
 public class Running : MonoBehaviour {
 
     public float acceleration;
 
     public Score score;
 
     private float seconds = 1;
 
     Animator animator;
     
     public bool dead = false;
     float deathCooldown;
 
     public Vector3 jumpVelocity;
     
 
     // used to get animations
     void Start () {
 
 
 
         animator = transform.GetComponentInChildren<Animator>();
 
         if (animator == null) {
                         Debug.LogError ("Didn't find animator!");
                 }
     }
     
     
     
     // Update is called once per frame
     void Update () {
         if (dead) {    
 
             seconds -= 1 * Time.deltaTime;
                         if (seconds <= 0) {
                                 Application.LoadLevel ("DeathScene");
 
                         }
                 } else {
                         if(Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown (0)) {
                         
                         }
                 }
     }
     
 
     void FixedUpdate() {
         if (dead)
                         return;
         //Run Speed
         transform.Translate (4.2f * Time.deltaTime, 0f, 0f);
     }
 
     //Collider to die
     void OnCollisionEnter2D(Collision2D collision) 
     {
 
         if (collision.collider.tag == "Obstacle" || collision.collider.tag == "ObstacleBag" || collision.collider.tag == "ObstacleGarbage") {
                         animator.SetTrigger ("Death");
                         dead = true;
                         audio.Play();
         }
 
         if (collision.collider.tag == "Ground" || (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown (0))) {
             rigidbody2D.AddForce (Vector3.up * 185);
             animator.SetTrigger ("DoJump");
             
         }
     }
 }
 
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

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by dreamhex · Sep 28, 2014 at 03:12 PM

I think it's something like this.

     public Transform groundCheck;
     bool grounded = false;
     public int jumpSpeed;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1<< LayerMask.NameToLayer("Ground"));
         if ((Input.GetKeyDown("space"))&&(grounded))
         {
             rigidbody2D.AddForce(transform.up*jumpSpeed);
             grounded = false; //so you can jump only once
         }
                             
     }


You'll make a new GameObject and place it under your Player (no collider needed on GameObject, that white thing is Player :D ). Also sprite which represents ground should have layer named "Ground". alt text


gameobject.png (1.8 kB)
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 braydon97 · Sep 28, 2014 at 09:19 PM 0
Share

I added the code ect. I can't have the ground layer set to Ground, as I have it set to Background so it loops the ground.

is there a way to make it a tag?

and could you be more specific on what to do with the gameobject stuff? thanks in advance.

so far the code works just I'm not able to jump at all

I think that is because of the Layer$$anonymous$$ask. I can't have it set Ground as a layer because if i did that it would not loop my background. is there another way to do it?

here is the code

 using UnityEngine;
 using System.Collections;
 
 public class Running : $$anonymous$$onoBehaviour {
 
     public float acceleration;
 
     public Score score;
 
     private float seconds = 1;
 
     Animator animator;
 
     //
 
     public Transform groundCheck;
     bool grounded = false;
     public int jumpSpeed;
 
     //
 
     public bool dead = false;
     float deathCooldown;
 
     public Vector3 jumpVelocity;
     
 
     // used to get animations
     void Start () {
 
 
 
         animator = transform.GetComponentInChildren<Animator>();
 
         if (animator == null) {
                         Debug.LogError ("Didn't find animator!");
                 }
     }
     
     
     
     // Update is called once per frame
     void Update () {
         if (dead) {    
 
                         seconds -= 1 * Time.deltaTime;
                         if (seconds <= 0) {
                                 Application.LoadLevel ("DeathScene");
 
                         }
                 } else {
                         grounded = Physics2D.Linecast (transform.position, groundCheck.position, 1 << Layer$$anonymous$$ask.NameToLayer ("Ground"));
                         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space) || Input.Get$$anonymous$$ouseButtonDown (0) && (grounded)) {
                                 rigidbody2D.AddForce (transform.up * 175);
                         animator.SetTrigger ("DoJump");
                         grounded = false;
                             }
 
                         }
                 }
     
 
     void FixedUpdate() {
         if (dead)
                         return;
         //Run Speed
         transform.Translate (4.2f * Time.deltaTime, 0f, 0f);
     }
 
     //Collider to die
     void OnCollisionEnter2D(Collision2D collision) {
         if (collision.collider.tag == "Obstacle" || collision.collider.tag == "ObstacleBag" || collision.collider.tag == "ObstacleGarbage") {
                         animator.SetTrigger ("Death");
                         dead = true;
                         audio.Play ();
                 }
         }
 }
 
avatar image Shiro_Rin · Oct 17, 2015 at 08:41 PM 0
Share

The grounded check works, however force isn't being added, so it won't jump ? Am I doing something wrong ?

avatar image Shiro_Rin Shiro_Rin · Oct 17, 2015 at 08:44 PM 0
Share

I fixed it

avatar image
0
Wiki

Answer by jmparavicini · Sep 28, 2014 at 01:47 PM

in java you could use if(is.Grounded) but im not sure if you can do the same in c#

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 Cherno · Sep 28, 2014 at 04:05 PM

Here is a tutorial that deals with basic character movement, including jumping.

https://www.packtpub.com/books/content/unity-3x-scripting-character-controller-versus-rigidbody

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 AfonsoLopez · Oct 06, 2016 at 07:52 AM

I have the same issue

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
 
     public Animator Anim;
     public Rigidbody2D PlayerRigidbody;
     public int Pulo;
 
     
     public bool punch;
 
     //verifacador de chao 
     public Transform GroundCheck;
     public bool grounded;
     public LayerMask whatisGround;
     
     //soco tempo
     public float socoTempo;
     public float tempoTempo;
 
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
     
         
         // se o pulo for apertado e estiver pisando no chao, faca isso
     if(Input.GetButtonDown("Jump") && grounded== true) {
             PlayerRigidbody.AddForce(new Vector2(0, Pulo));
             
         }
 
     if(Input.GetButtonDown("Fire2")) {
             punch = true;
             tempoTempo = 0;
 
         }
 
         grounded = Physics2D.OverlapCircle(GroundCheck.position, 0.2f, whatisGround) ;
 
         if(punch == true)
         {
             tempoTempo += Time.deltaTime;
             if(tempoTempo >= socoTempo)
             {
                 punch = false;
             }
         }
 
         Anim.SetBool ("soco", punch);
 
     }
 }
 
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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Simple C# script to move a gameObject toward the direction of the player at the instance of it's spawning. 1 Answer

C# 2D InfiniteRunner run with same speed even when going up 1 Answer

Multiple jump C# Script. 0 Answers

2D Platformer Jump while Running Android 1 Answer

C#: Jump function, grounded stays True 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