#Y Velocity increases when my player is running.

1 messages · Page 1 of 1 (latest)

dusk kiln
#

Code:

#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
private int speed = 20;
private float jumpStrength = 30;
private float horiInput;
private bool isFacingRight = true;

[SerializeField] Rigidbody2D rigid;
[SerializeField] Transform groundCheck;
[SerializeField] LayerMask groundLayer;
[SerializeField] Animator animation;


// Update is called once per frame
private void Update()
{
    Debug.Log(rigid.velocity.y);
    horiInput = Input.GetAxis("Horizontal");
    flip();
    // Jump button
    if (Input.GetKeyDown(KeyCode.Space) == true && isGrounded())
    {
        rigid.velocity = new Vector2(rigid.velocity.x, jumpStrength);
    }
    // Decreases jump height if player lets go early
    if (Input.GetKeyUp(KeyCode.Space))
    {
        rigid.velocity = new Vector2(rigid.velocity.x, rigid.velocity.y * 0.5f);
    }
    // sets running animation
    if (horiInput != 0)
    {
        animation.SetBool("isRunning", true);
    }
    else
    {
        animation.SetBool("isRunning", false);
        rigid.velocity = new Vector2(0, rigid.velocity.y);
    }
    // Sets jumping animation
    if (rigid.velocity.y > 0)
    {
        animation.SetBool("IsJumpingUp", true);
    }
    else
    {
        animation.SetBool("IsJumpingUp", false);
    }
    // Sets falling animation
    if (rigid.velocity.y < 0)
    {
        animation.SetBool("IsJumpingDown", true);
    }
    else
    {
        animation.SetBool("IsJumpingDown", false);
    }

}
#

// Moves the player left and right
private void FixedUpdate()
{
rigid.velocity = new Vector2(speed * horiInput, rigid.velocity.y);
}
// Checks if player is grounded
private bool isGrounded()
{
return Physics2D.OverlapCircle(groundCheck.transform.position, 0.2f, groundLayer);
}
// Flips player if they are looking and moving in opposite ways
private void flip()
{
if ((isFacingRight && horiInput < 0) || (!isFacingRight && horiInput > 0))
{
isFacingRight = !isFacingRight;
Vector3 localScale = transform.localScale;
localScale.x = localScale.x * -1f;
transform.localScale = localScale;
}
}
}

dusk kiln
#

Ayy that worked thanks