I have been working on a crouch function for my 2d side scroller. It works,but it stops the attack function from working. Also I have tried to add jumping in there, but I couldn’t figure it out. Could some one please help me?
using UnityEngine;
using System.Collections;
using SpriteFactory;
public class player : MonoBehaviour {
public float speed = 1.0f;
public float jump = 10.0f;
public float gravity = 10.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;
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;
}
}