I added or tried to add a jump function similar to how I added the move function. I also tried it the same way I added crouch/attack function. It doesn’t do anything though he just sits still. Sometimes I can get it to work, but if I tap the button he barely moves up and the animation doesn’t continue. I tried GetButtonUp but that doesn’t work either.
Lines with asterisk symbols is what I tried to do to add jumping.
heres the code with jump function in it:
public float speed = 1.0f;
*****public float jumpspeed = 1.0f;******
private Transform thisTransform;
private Sprite sprite;
private bool flipped;
// Use this for initialization
void Awake () {
thisTransform = transform;
sprite = (Sprite)GetComponent(typeof(Sprite));
}
// Update is called once per frame
void Update () {
Vector3 moveVector = new Vector3(0,0,0);
bool moving = false;
************bool jumping = false;*************
*********if(Input.GetKey(KeyCode.Space)){
moveVector.y += 2.0f;
jumping = false;
}*************
if(Input.GetKey(KeyCode.A)){
moveVector.x -= 1.0f;
moving = true;
}
if(Input.GetKey(KeyCode.D)){
moveVector.x += 1.0f;
moving = true;
}
if(Input.GetKey(KeyCode.Mouse0)){
Attack();
} else {
if(!IsAttacking()) {
if(moving) {
Move(moveVector);
} else {
Stand();
}
}
if(Input.GetKey(KeyCode.S)){
Crouch();
} else {
if(!IsCrouching()) {
}
if(moving) {
Move(moveVector);
}
else {
Stand();
}
}
}
}
private void Stand() {
sprite.Stop();
}
private void Move(Vector3 moveVector) {
FlipSprite(moveVector.x);
moveVector *= speed * Time.deltaTime;
thisTransform.position = thisTransform.position + moveVector;
sprite.Play("walk");
}
private void FlipSprite(float x) {
if(x == 0.0f) return;
float xDir = Mathf.Sign(x);
if(!flipped && xDir < 0.0f) {
flipped = true;
sprite.SetFlippedState(true, false);
} else if(flipped&& xDir > 0.0f) {
flipped = false;
sprite.SetFlippedState(false, false);
}
}
private void Attack() {
if(IsAttacking()) return;
sprite.Play("Sword");
}
private bool IsAttacking() {
if(sprite.IsAnimationPlaying("Sword")){ return false;}
return false;
}
private void Crouch() {
if(IsCrouching()) return;
sprite.Play("crouch");
}
private bool IsCrouching() {
if(sprite.IsAnimationPlaying("crouch")) {return false;}
return false;
}
************private void Jump(Vector3 moveVector) {
FlipSprite(moveVector.x);
moveVector *= jumpspeed * Time.deltaTime;
thisTransform.position = thisTransform.position + moveVector;
sprite.Play("jump");****************
}
}