Hi everybody,
i know the question have been asked an incredibly amount of time but after my research i couldn’t find a case similar to mine.
I explain you the problem, i’m a debutant and maybe my script is total garbagge but for a while it was working fine.
I working on a 2d plateformer.
I actually try to make a directional jump for my character, when the player run into a direction then the jump button get pressed he jump in this direction. I was able to make him move and jump, to resolve the infinite jump problem i use a bool isGrounded, but now it seem if i want my player to have directionnal jump , i can’t make him move while is in the air.
Here is my code.
using UnityEngine;
using System.Collections;
public class Control2d : MonoBehaviour {
// Use this for initialization
int i;
int j;
public float Height = 30;
public float speed = 10;
bool isGrounded;
bool goRight;
bool goLeft;
// Update is called once per frame
void Update () {
if (isGrounded == true) {
if (Input.GetAxis ("Horizontal") > 0 & Input.GetKey (KeyCode.Space)) {
Debug.Log ("Jump");
rigidbody2D.velocity = new Vector2 (speed, Height);
}
if (Input.GetAxis ("Horizontal") < 0 & Input.GetKey (KeyCode.Space)) {
Debug.Log ("Jump");
rigidbody2D.velocity = new Vector2 (-speed, Height);
}
if (Input.GetKey (KeyCode.Space)) {
Debug.Log ("Jump");
isGrounded = false;
Debug.Log ("isGrounded = false");
rigidbody2D.velocity = new Vector2 (0, Height);
}
if (Input.GetAxis ("Horizontal") > 0) {
Debug.Log ("Right");
rigidbody2D.velocity = new Vector2 (speed, 0);
}
if (Input.GetAxis ("Horizontal") < 0) {
goLeft = true;
Debug.Log ("Left");
rigidbody2D.velocity = new Vector2 (-speed, 0);
}
}
if (isGrounded == false) {
if (Input.GetAxis ("Horizontal") > 0) {
Debug.Log ("Right");
rigidbody2D.velocity = new Vector2 (speed, 0);
}
if (Input.GetAxis ("Horizontal") < 0) {
goLeft = true;
Debug.Log ("Left");
rigidbody2D.velocity = new Vector2 (-speed, 0);
}
}
}
void OnCollisionEnter2D(Collision2D coll){
if (coll.gameObject.tag == "Floor")
{
isGrounded = true;
Debug.Log ("isGrouded == true");
}
}
}
I tried to move the differente condition outside the if statement but it never worked, So if anyone figure where is the problem, i will be very helpful
Also sorry for my bad english .