- Home /
Question by
rashad · Dec 17, 2019 at 04:47 PM ·
movementanimatorplayer movement
Player can't move but the animations plays
Hello,
I'm working on my player controller for this 2D sidescroller game I'm working on. The Issue I'm having is that I have complete control over the animations when I'm moving left of right and the player has the ability to jump but it not able to move.
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
CanMove = !CanMove;
}
// Update is called once per frame
void Update()
{
Movement();
DashAttack();
Jump();
}
public void Movement()
{
movInput = Input.GetAxisRaw("Horizontal");
//Anim
if (movInput > 0.1f || movInput < -0.1f)
{
anim.SetBool("isMoving", true);
}
else
{
anim.SetBool("isMoving", false);
}
//Movements
rb.velocity = new Vector2(movInput * MovementSpeed, rb.velocity.y);
FlipCharacter(movInput);
}
//Will Fix Later
public void DashAttack()
{
if ((Input.GetKeyDown(KeyCode.Period) || Input.GetButtonDown("X_Button") )&& IsGrounded == true)
{
Debug.Log("X Key is Pressed");
//Play Animation
UsingDashAttack = !UsingDashAttack;
anim.SetBool("useDashAttk", true);
DashCol_L.enabled = true;
if (movInput < 0)
{
rb.AddForce(new Vector2(-DashForce, 0));
}
else if (movInput > 0)
{
rb.AddForce(new Vector2(DashForce, 0));
}
}
else
{
anim.SetBool("useDashAttk", false);
DashCol_L.enabled = false;
}
}
public void Jump()
{
if ((Input.GetButtonDown("Jump") || Input.GetButtonDown("A_Button")) && IsGrounded == true)
{
//Play Anim
anim.SetBool("isJumping", true);
//Get reference to Rigibody to AddForce to create a 'Jump'
rb.AddForce(new Vector2(0f, JumpHeight), ForceMode2D.Impulse);
}
else
{
anim.SetBool("isJumping", false);
}
}
public void PlayerLost()
{
anim.SetTrigger("Dead");
PlayerBody.enabled = false;
Gcheck.enabled = false;
CanMove = false;
GameManager.gm.Lives -= 1;
}
public void FlipCharacter(float movInput)
{
//If Facing Left or Right, Player should turn in that direction.
if (movInput > 0 && IsFacingLeft == false || movInput < 0 && IsFacingLeft == true)
{
IsFacingLeft = !IsFacingLeft;
Vector3 charcterScale = transform.localScale;
charcterScale *= 1;
transform.localScale = charcterScale;
}
}
//Collision Checks
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.tag == "Ground")
{
IsGrounded = true;
CanMove = true;
anim.SetBool("isGrounded", true);
}
if (collision.collider.tag == "Enemy")
{
PlayerLost();
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.collider.tag == "Ground")
{
IsGrounded = false;
anim.SetBool("isGrounded", false);
}
}
}
Comment
Use debugging to figure out what is wrong and narrow down the problem.
I used some break points where the movements is being played and it reads but it doesn't respond in editor.