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 Ragswolf · May 24, 2014 at 05:33 AM · raycastjumpresetgroundeddoublejump

Jumps never reset

Hey this is my first time posting here so I hope everything is in order, anyway, I have a problem with a couple scripts that deal with controlling the character and I can't spot the problem involving my player character never being able to jump again after double jumping the first time, at first I thought it may have been a layering problem but I've double checked the layering on my sprites and I'm certain it's all fine; now I'm wondering if it's a ray-cast/groundcheck problem but I can't tell. (ray-casts are at line 34, line 90).

using UnityEngine; using System.Collections;

 public class Character : MonoBehaviour 
 {
 
     public enum inputState 
     { 
         None, 
         WalkLeft, 
         WalkRight, 
         Jump, 
     }
     [HideInInspector] public inputState currentInputState;
     
     [HideInInspector] public enum facing { Right, Left }
     [HideInInspector] public facing facingDir;
 
     [HideInInspector] public bool alive = true;
     
     protected Transform _transform;
     protected Rigidbody2D _rigidbody;
 
     // edit these to tune character movement    
     private float runVel = 8f;     // run speed
     private float walkVel = 6f;     // walk speed 
     private float jumpVel = 8f;     // jump velocity
     private float jump2Vel = 6f;     // double jump velocity
     private float fallVel = 2f;        // fall velocity, gravity
 
     private float moveVel;
     private float pVel = 0f;
     
     private int jumps = 0;
     private int maxJumps = 2;         // set to 2 for double jump
         
     protected string team = "";
 
     // raycast stuff
     private RaycastHit2D hit;
     private Vector2 physVel = new Vector2();
     [HideInInspector] public bool grounded = false;
     private int groundMask = 1 << 8; // Ground layer mask
 
     public virtual void Awake()
     {
         _transform = transform;
         _rigidbody = rigidbody2D;
     }
     
     // Use this for initialization
     public virtual void Start () 
     {
         moveVel = walkVel;
     }
     
     
     // ============================== FIXEDUPDATE ============================== 
 
     public virtual void UpdatePhysics()
     {
 
         physVel = Vector2.zero;
 
         // move left
         if(currentInputState == inputState.WalkLeft)
         {
             physVel.x = -moveVel;
         }
 
         // move right
         if(currentInputState == inputState.WalkRight)
         {
             physVel.x = moveVel;
         }
 
         // jump
         if(currentInputState == inputState.Jump)
         {
             if(jumps < maxJumps)
             {
                 jumps += 1;
                 if(jumps == 1)
                 {
                     _rigidbody.velocity = new Vector2(physVel.x, jumpVel);
                 }
                 else if(jumps == 2)
                 {
                     _rigidbody.velocity = new Vector2(physVel.x, jump2Vel);
                 }
             }
         }
 
 
         // use raycasts to determine if the player is standing on the ground or not
         if (Physics2D.Raycast(new Vector2(_transform.position.x-0.1f,_transform.position.y), -Vector2.up, .26f, groundMask) 
             || Physics2D.Raycast(new Vector2(_transform.position.x+0.1f,_transform.position.y), -Vector2.up, .26f, groundMask))
         {
             grounded = true;
             jumps = 0;
         }
         else
         {
             grounded = false;
             _rigidbody.AddForce(-Vector2.up * fallVel);
         }
 
         // actually move the player
         _rigidbody.velocity = new Vector2(physVel.x, _rigidbody.velocity.y);
     }
 
 
 public enum MyTeam 
 {
     Team1,
     Team2,
     None
 }

}

 using UnityEngine;
 using System.Collections;
 
 public class Player1 : Character 
 {    
     // Use this for initialization
     public override void Start () 
     {
         base.Start();
 
         // grab the players position at startup and use it for the spawn position when starting new rounds
         spawnPos = _transform.position;
     }
 
     public void FixedUpdate()
     {
         // inputstate is none unless one of the movement keys are pressed
         currentInputState = inputState.None;
         
         // move left
         if(Input.GetKey(KeyCode.A)) 
         { 
             currentInputState = inputState.WalkLeft;
             facingDir = facing.Left;
         }
 
         // move right
         if (Input.GetKey(KeyCode.D) && currentInputState != inputState.WalkLeft) 
         { 
             currentInputState = inputState.WalkRight;
             facingDir = facing.Right;
         }
 
         // jump
         if (Input.GetKeyDown(KeyCode.W)) 
         { 
             currentInputState = inputState.Jump;
         }
 
         UpdatePhysics();
     }
 
         }}
Comment
Add comment · Show 3
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 Fattie · May 24, 2014 at 07:29 AM 1
Share

use Debug.Log to solve problems like this.

avatar image vvkkumar06 · May 24, 2014 at 07:40 AM 0
Share

Are you using some animation for jump?

avatar image Ragswolf · May 25, 2014 at 06:18 PM 0
Share

No I don't have an animation, but I could have one, why?

2 Replies

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

Answer by Ganadote · May 25, 2014 at 07:46 PM

In the update function, I would add Debug.Log(jump); This will allow you to see what the jump is set to and if it is resetting.

Comment
Add comment · Show 2 · 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 Ragswolf · May 25, 2014 at 09:01 PM 0
Share

Thank you.

avatar image Ragswolf · May 25, 2014 at 11:22 PM 0
Share

I just checked out the problem using Debug.Log (jumps); and i got this:

2 UnityEngine.Debug:Log(Object) Character:UpdatePhysics() (at Assets/Character.cs:90) <- this is the Debug log. Player1:FixedUpdate() (at Assets/Player1.cs:32) <- this is "UpdatePhysics();" from the 2nd script. I guess the problem is related to UpdatePhysics then?

avatar image
0

Answer by sandbaydev · May 25, 2014 at 07:10 PM

Like Fattie mentioned, start adding debug logs and/or remove those "hideInInspector" away from "grounded" and check what exactly is going on.

I don't know 2D, but are you adding y velocity correctly? looks that on the first jump the velocity is +8f, but if you do another jump velocity will change to +6f. Is this correct?

Happy hunting.

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 Ragswolf · May 25, 2014 at 09:01 PM 0
Share

Thank you so much, I'm trying all these suggestions. And yes the second jump has a lower height than the first.

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

25 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

Related Questions

issues with jumping and linecast2d and raycast2d 0 Answers

How to determine if the player can jump, without using raycasts. (2D) 1 Answer

grouding down 0 Answers

Let Character Controller jump. 0 Answers

RayCast to test if player is grounded 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