- Home /
Question by
LelandGMC · Jun 05, 2015 at 04:11 PM ·
unity 5programming
2d Platformer Jump problems
I've been building the code to make the player jump
The issues were
The player jumps 2 times but locks up afterwards
The Player jumps continually instead jump twice then do thing again at any time
Here is the codes:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
//Stored Game Data - Private
private bool facingRight = true;
[SerializeField] private float Speed = 10f;
[SerializeField] private float JumpStrength = 450f;
[SerializeField] private bool airManipulate = false;
//Stored Game Data 2- Private
private bool grounded = false;
private int jumpCounter = 0;
void awake()
{
}
private void FixedUpdate()
{
//// Reset double jump
//if (grounded)
//{
// doubleJump = false;
//}
}
public void Movement(float move, bool jump)
{
//Air Control
if (grounded || airManipulate)
{
//Move the Character/Characters
GetComponent<Rigidbody2D>().velocity = new Vector2(move * Speed, GetComponent<Rigidbody2D>().velocity.y);
if (move > 0 && !facingRight)
Flip();
else if (move < 0 && facingRight)
Flip();
}
//Disable double jumping after being used
//Should the player to jump...
if (grounded && jumpCounter !=0)
{
//grounded = true;
//doubleJump = false;
////Reset vertical velocity when double jumping
//GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, 0);
jumpCounter = 0;
}
//Disable double jumping after being used
if((grounded && jump) || (!grounded && jumpCounter < 2 && jump)){
GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, JumpStrength));
grounded = false;
jumpCounter+=1;
}
}
private void Flip()
{
//Switch the way the player is labelled
facingRight = !facingRight;
//Multiply
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
And this is the one for PlayerControl:
using UnityEngine; using System.Collections;
//It will need an Script "Player"
[RequireComponent(typeof (Player))]
public class PlayerControl : MonoBehaviour {
private Player character;
private bool jump;
private void Awake()
{
character = GetComponent<Player>();
}
private void Update()
{
if (Input.GetButtonDown("Jump"))
jump = true;
}
private void FixedUpdate()
{
character.Movement(1, jump);
jump = false;
}
}
Any suggestions on how to make the player jump twice without it locking up?
Comment