Question by
FlyingRonin · Aug 19, 2020 at 11:30 AM ·
jumpingplayer movement
player jumping issue
public class Player : MonoBehaviour { [SerializeField] private Animator animator;
public int maxHp = 100;
public int currentHp;
public StaminaAndHpHandler healthBar;
public StaminaAndHpHandler staminaHandler;
public bool isRunning = false;
private int sprintTime = 1;
[SerializeField]
private float _speed = 3f;
private float _horizontalSpeed;
private float _horizontalMovement;
[SerializeField]
private UIManager _uiManager;
[SerializeField]
private PlayerCombat _playerCombat;
[SerializeField]
private object boxCollider2D;
[SerializeField]
private new Rigidbody2D rigidbody;
[SerializeField]
private Rigidbody2D rb2d;
private bool isGrounded;
public Transform feetPos;
public float checkRadius;
public LayerMask whatIsGround;
public float jumpForce = 10f;
void Awake()
{
rb2d = transform.GetComponent<Rigidbody2D>();
}
void Start()
{
currentHp = maxHp;
healthBar.SetMaxHealth(maxHp);
rigidbody = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
PlayerMovement();
}
void Update()
{
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
rb2d.velocity = Vector2.up * jumpForce;
}
}
void PlayerMovement()
{
if (Input.GetKey(KeyCode.LeftShift) && _horizontalSpeed != 0 && staminaHandler.currentStamina > 0 )
{
isRunning = true;
animator.SetBool("Run", true);
staminaHandler.Running(sprintTime);
_speed = 5f;
}
else
{
isRunning = false;
animator.SetBool("Run", false);
_speed = 3f;
}
_horizontalSpeed = Input.GetAxisRaw("Horizontal") * _speed;
Vector3 _horizontalMovement = new Vector3(Input.GetAxis("Horizontal"), rb2d.velocity.y, 0f).normalized;
animator.SetFloat("Speed", Mathf.Abs(_horizontalSpeed));
if (!this.animator.GetCurrentAnimatorStateInfo(0).IsTag("Attack"))
{
rigidbody.MovePosition ((Vector3)transform.position + (_horizontalMovement * _speed * Time.fixedDeltaTime));
}
//Stops movement when attacking
Vector3 characterOrientation = transform.localScale;
if (Input.GetAxis("Horizontal") < 0)
{
characterOrientation.x = -1;
}
if (Input.GetAxis("Horizontal") > 0)
{
characterOrientation.x = 1;
}
transform.localScale = characterOrientation;
}
public void DamageTaken(int damage)
{
currentHp -= damage;
healthBar.SetHealth(currentHp);
}
}
this is my entire player code since i can't seem to be able to find the issue. Basically i try to jump but the character is always glued to the ground no matter what except when i click attack.
also i've tried multiple variations but didn't work well either so maybe a change in how my movement is detected is needed...idk
Comment
Answer by FlyingRonin · Aug 19, 2020 at 09:25 AM
Well I'm not exactly asking for a debug. Thanks anyways.