Question by
Karios · Oct 27, 2015 at 08:45 AM ·
2d-platformer2d-physics2d animation
I want to implement double jump function on my character but everything i try fail
i have two scripts one that add force to player that makes him jump once and then another scripts with call the jump and play the jump function.
Making the player Jump Method
private bool inAir = false;
public bool jumpPress = false;
public float _Jumpspeed;
void Update ()
{
if (!inAir && Mathf.Abs(this.GetComponent<Rigidbody2D>().velocity.y) > _Jumpspeed)
{
inAir = true;
} else if(inAir && this.GetComponent<Rigidbody2D>().velocity.y == 0.00f){
inAir =false;
if (jumpPress)
{
jump();
}
}
}
public void jump(){
jumpPress = true;
if (inAir)return;
this.GetComponent<Rigidbody2D>().AddForce (Vector2.up * 4000);
}
And this one handle the touch input and button in put'
private bool isMobile = true;
private playerHandler _player;
public GameObject shot;
public Transform shotSpawn;
public float fireRate;
private float nextFire;
void Start()
{
if (Application.isEditor)
isMobile = false;
_player = GameObject.Find("Player").GetComponent<playerHandler>();
}
// Update is called once per frame
void Update()
{
if (isMobile)
{
int tmpC = Input.touchCount;
tmpC--;
//This Statement will let the player jump when the left side is pressed
if (Input.GetTouch(tmpC).phase == TouchPhase.Began)
{
if (Input.touchCount == 1)
{
Touch touch = Input.touches[0];
if (touch.position.x < Screen.width / 2)
handleInteraction(true);
}
}
//This Statement will let the player fire when pressed
if (Input.GetTouch(tmpC).phase == TouchPhase.Began && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
if (Input.touchCount == 1)
{
Touch touch = Input.touches[0];
if (touch.position.x > Screen.width / 2)
// handleInteraction(true);
//fire projectile
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
// Handheld.Vibrate();
Debug.Log("Device Vibrating");
}
}
if (Input.GetTouch(tmpC).phase == TouchPhase.Ended)
{
handleInteraction(false);
}
}
else
{
if (Input.GetButton("shot") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
}
if (Input.GetMouseButtonDown(0))
{
handleInteraction(true);
}
if (Input.GetMouseButtonUp(0))
{
handleInteraction(false);
}
}
}
void handleInteraction(bool starting)
{
int x = 2;
if (starting )
{
_player.jump();
}
else
{
_player.jumpPress = false;
}
}
}
Comment