- Home /
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();
}
}}
No I don't have an animation, but I could have one, why?
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.
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?
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.
Thank you so much, I'm trying all these suggestions. And yes the second jump has a lower height than the first.
Your answer
Follow this Question
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