- Home /
2d character flipping and conditionally walking backwards
Hi,
I'm trying to build a platformer where the character can face left and right while his sword is sheathed, and only face right, (walking backwards to left if needed) when he has it's drawn. Instead he either doesn't care if the sword is drawn or just doesn't flip at all. All help appreciated.
The code i used is like below:
public class playerMovement : MonoBehaviour {
public float maxSpeed = 1.5f;
bool facingRight = true;
bool drawsword = false;
void Start () {
anim = GetComponent<Animator> ();
rb2D = GetComponent<Rigidbody2D> ();
}
void FixedUpdate ()
{
float move = Input.GetAxis ("Horizontal");
rb2D.velocity = new Vector2 (move * maxSpeed, rb2D.velocity.y);
bool drawsword = Input.GetButtonDown ("Fire1");
if (drawsword) {
anim.SetBool ("drawsword", true);
}
if (drawsword = true && Input.GetKeyDown (KeyCode.R)) {
anim.SetBool ("drawsword", false);
if (drawsword = false && move > 0 && !facingRight)
Flip ();
else if (drawsword = false && move < 0 && facingRight)
Flip ();
}
void Flip ()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
Comment