- Home /
Figured it out
2D Input.GetAxis not recognized when local scale is flipped
I am trying to make a running attack and a stationary attack for my player character, It works when I am facing to the right but when I flip my character to the left when moving left it doesn't recognize the Input.GetAxis float value which doesn't make the isRunning variable true. I want my character to preform "attackOne" when stationary and "attackTwo" when running in either direction(left or right).
This is the my code can anyone help me out or know why it is happening?:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float maxSpeed = 18.0f;
public bool facingRight = true;
public bool isGrounded = false;
public Transform groundCheck;
public float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float jumpForce = 1300.0f;
public bool isDead = false;
public Collider2D attackTrigger;
public Collider2D attackTriggerTwo;
private bool attackOneComplete = true;
private bool attackTwoComplete = true;
private bool isRunning = false;
private Animator anim;
void Start ()
{
anim = GetComponent<Animator>();
isDead = false;
}
void FixedUpdate ()
{
if(isDead == false)
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
anim.SetBool("Ground", isGrounded);
float move = Input.GetAxis("Horizontal");
anim.SetFloat("Speed", Mathf.Abs(move));
rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
if(move > 0.01f)
{
isRunning = true;
}
else if(move < 0.01f)
{
isRunning = false;
}
if(move > 0 && !facingRight)
{
FlipFacing();
}
else if(move <0 && facingRight)
{
FlipFacing();
}
}
}
void Update()
{
if(isGrounded && Input.GetButtonDown("Jump") && isDead == false)
{
anim.SetBool("Ground", false);
rigidbody2D.AddForce(new Vector2(0, jumpForce));
}
if(Input.GetButtonDown("Fire1") && attackOneComplete == true && isRunning == false && isDead == false)
{
StartCoroutine("AttackModeOne");
}
if(Input.GetButtonDown("Fire1") && attackTwoComplete == true && isRunning == true && isDead == false)
{
StartCoroutine("AttackModeTwo");
}
}
void FlipFacing()
{
facingRight = !facingRight;
Vector3 charScale = transform.localScale;
charScale.x *= -1;
transform.localScale = charScale;
}
IEnumerator AttackModeOne()
{
attackOneComplete = false;
anim.SetTrigger("Attack");
yield return new WaitForSeconds(0.2f);
attackTrigger.enabled = true;
attackOneComplete = true;
yield return new WaitForSeconds(0.1f);
attackTrigger.enabled = false;
}
IEnumerator AttackModeTwo()
{
attackTwoComplete = false;
anim.SetTrigger("Attack2");
yield return new WaitForSeconds(0.2f);
attackTriggerTwo.enabled = true;
yield return new WaitForSeconds(0.7f);
attackTwoComplete = true;
attackTriggerTwo.enabled = false;
}
}
Not sure if this help or not with your issue, but the shader used for Sprites is two-sided. So you can flip your character by rotating 180 degrees on the 'y' rather than setting the local scale to a negative value (assu$$anonymous$$g your anchor is in the middle).
Thanks for the reply. Yeah I have that on my enemy characters but my player is setup to where if I did that I would mess up the sprite. But I did find a way around it by adding:
if(rigidbody2D.velocity.magnitude > 0)
{
isRunning = true
}
and so on... So I will close the question.