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();
}
}}