Hi gentlemen,
I’ve been going nuts over what should be an easy as hell script. On my 2D project i have my character able to walk and run towards right and left. But i don’t want him to be able to run left when his sword is drawn since the enemies only come from right edge of the screen. Here is the script i use:
public class playerMovement : MonoBehaviour {
public float maxSpeed = 2f;
public float multiplier = 3f;
Animator anim;
Rigidbody2D rb2D;
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 = (rb2D.transform.localScale.x > 0) && Input.GetButtonDown("Fire1");
bool run = (Input.GetButton("Fire3"));
if (move > 0)
anim.SetBool("walkRight", true);
else
anim.SetBool("walkRight", false);
if (move < 0)
{
anim.SetBool("walkLeft", true);
}
else
anim.SetBool("walkLeft", false);
if (drawSword)
{
anim.SetBool("drawSword", true);
}
if (drawSword = true && Input.GetKeyDown(KeyCode.R))
{
anim.SetBool("drawSword", false);
}
if (run && move < 0)
{
rb2D.velocity = new Vector2(move * maxSpeed * multiplier, rb2D.velocity.y);
anim.SetBool("run", true);
}
if (run && move > 0)
{
if (drawSword == true)
{
anim.SetBool("run", false);
rb2D.velocity = new Vector2(move * maxSpeed, rb2D.velocity.y);
}
else
{
anim.SetBool("run", true);
rb2D.velocity = new Vector2(move * maxSpeed * multiplier, rb2D.velocity.y);
}
}
else
{
rb2D.velocity = new Vector2(move * maxSpeed, rb2D.velocity.y);
anim.SetBool("run", false);
}
}
}
With that script he runs right just fine but never runs left no matter if the drawSword is true or false.