make 2d character move direction its facing

using UnityEngine;
using System.Collections;

public class Player_movement : MonoBehaviour {
void OnCollisionEnter2D(Collision2D coll){
if (coll.gameObject.tag == “level” ) {
isjumping = false;
hasdashed = false;
}
}
void dash (){
rgb.AddForce (transform.forward * dashspeed);
Debug.Log (“dashing”);
}
public float speed = 3f;
public float jumpspeed = 90f;
public void jump () {
rgb.AddForce (transform.up * jumpspeed);
isjumping = true;

}
private Rigidbody2D rgb;
private bool isjumping;
private bool hasdashed;
public float dashspeed = 90f;
// Use this for initialization
void Start () {
	rgb = gameObject.GetComponent<Rigidbody2D>();
	isjumping = false;
	hasdashed = false;
}

// Update is called once per frame
void Update () {
	if (Input.GetKey (KeyCode.A)) {
		transform.position += Vector3.left * speed * Time.deltaTime;
	}
	if (Input.GetKey (KeyCode.D)) {
		transform.position += Vector3.right * speed * Time.deltaTime;
	}
	if (Input.GetKeyDown(KeyCode.Space) && !isjumping && !hasdashed ) {
		jump ();
	}
	if (Input.GetKeyDown (KeyCode.Space) && isjumping == true && !hasdashed) {
		dash ();
	}
}

}

i have this code that is made to make the character move and such, the problem i am having is with the dash and the if statement surrounding it. not only does the debug.log tell me that its activating when it shouldnt be even with my if statement perameters but also when i do activate it it doesnt do anything. can someone help?

Greetings friend!

My guess would be that dash() funciton executes first time you press space in order to jump().


It is because you firstly run Jump() method which changes the isjumping = true, then you check whether character is jumping and you perform dash(). Simply change order of execution and you should be good to go. As it is 2D game you shouldn’t be using Vector3.forward - it relates to Z Axis (third dimension).

I believe dash is supposed to quickly move your character in the direction this character is facing (left/right). Make use of Vector3.left / Vector3.right :slight_smile: