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 x147836 · Jan 19, 2015 at 08:09 AM · c#jumpbeginnerground

Jump/grounding bugs, advice appreciated

I should first volunteer I'm pretty new to both unity and programming in general (not to mention c# in specific), so apologies if I'm missing something obvious.

I'm creating a simple 2d platformer very much in the Mario style and am running into some bugs when making my character jump. I've written two scripts, one for controlling left/right movement and adding force to make the character jump, and a second using raycasting to determine if the player is grounded (and hence able to jump). Please see the code below.

I have two issues. One, between pressing play and entering the game state I seem to be able to store several jump inputs, such that my character jumps much higher at initialization. I assume this is the case because for some reason my grounding script is kicking in later than my movement script. Any advice on how to avoid this and if the player would experience the same or its just a quirk of the editor? I have noticed this same ability to store several inputs occurring randomly during gameplay as well, associated with a frame rate hitch usually (I'd note that I'm working on a high end machine and using only very low res sprites at this point, with few game objects in the scene).

Two, at seemingly random points my jump input doesn't seem to register. I haven't noticed any pattern, but have noticed it several times.

Any help the community could offer would be greatly appreciated.

Movement Script

 using UnityEngine;
 using System.Collections;
 
 public class PlayerControllerMovement : MonoBehaviour {
 
     public AudioClip jumpSmall;                                                        
     private float moveSpeed = 1.6f;                                                    
     private int jumpForce = 200;
 
     private Animator animPlayer;                                                    
     private PlayerGround playerGround;                                                
 
     void Movement() {                                                                
         animPlayer.SetFloat("speed", Mathf.Abs(Input.GetAxis("Horizontal")));        
         if(Input.GetAxisRaw("Horizontal") > 0) {                                    
             transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);        
             transform.eulerAngles = new Vector2 (0,0);                                
         }
         if(Input.GetAxisRaw("Horizontal") < 0) {
             transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);        
             transform.eulerAngles = new Vector2 (0,180);                            
             }
     }
 
     void Jump() {
         animPlayer.SetBool("isGrounded", playerGround.isGrounded);                    
         if(playerGround.isGrounded == true && Input.GetButtonDown("Jump")) {                            
             rigidbody2D.AddForce(Vector2.up * jumpForce);
             audio.PlayOneShot(jumpSmall);
         } 
     }
 
     void Start () {
         animPlayer = GetComponent<Animator>();                                        
         playerGround = GetComponent<PlayerGround>();                                
     }                                                                                
     
     void Update () {
         Movement();
     }
 
     void FixedUpdate () {
         Jump();
     }
 }

Ground Script

 using UnityEngine;
 using System.Collections;
 
 public class PlayerGround : MonoBehaviour {
 
     public bool isGrounded;                                                                        
     public float jumpRayLength = 0.025f;                                                        
     public Transform leftGCheck;
     public Transform rightGCheck;
     public Transform midGCheck;
 
     void Raycasting() {
         RaycastHit2D hit1 = Physics2D.Raycast(leftGCheck.position, -Vector2.up, jumpRayLength);            
         RaycastHit2D hit2 = Physics2D.Raycast(rightGCheck.position, -Vector2.up, jumpRayLength);
         RaycastHit2D hit3 = Physics2D.Raycast(midGCheck.position, -Vector2.up, jumpRayLength);
         Debug.DrawRay(leftGCheck.position, -Vector2.up * jumpRayLength, Color.yellow);                    
         Debug.DrawRay(rightGCheck.position, -Vector2.up * jumpRayLength, Color.yellow);
         Debug.DrawRay(midGCheck.position, -Vector2.up * jumpRayLength, Color.yellow);
         if(hit1.collider != null || hit2.collider != null || hit3.collider != null) {                    
             isGrounded = true;
         } else {
             isGrounded = false;
         }
     }
         
     void FixedUpdate () {
         Raycasting();
     }
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mangas · Jan 19, 2015 at 08:32 AM

For your second issue you just have to put Jump() in Update(). FixedUpdate() is mostly used to deal with physics, it's called in a fixed framerate and you risk missing a button press if you miss the frame, any input should be in Update(), which is called every frame.

This explains the order of execution of every update method: http://docs.unity3d.com/es/current/Manual/ExecutionOrder.html

The first issue you have may be related with the FixedUpdate() as well, or you could try to change the Script Execution Order in Edit -> Project Settings -> Script Execution Order.

Comment
Add comment · Show 1 · 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 x147836 · Jan 19, 2015 at 08:45 AM 0
Share

Big thanks $$anonymous$$angas, moving Jump to Update seems to have solved all of my problems. I had placed it in Update very early in writing the script, but moved it to FixedUpdate after I read that any code affecting a Rigidbody (like the addforce Jump applies) should be placed there. I guess I misunderstood the documentation somehow.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Super Ghouls 'n Ghosts style jumps & double jumps? 1 Answer

Ground Detection Lagging - 2D 2 Answers

My jumping script isn't working, And it isn't giving me any errors,My character isn't jumping, But I don't get any errors 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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