Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Player : MonoBehaviour
- {
- private Rigidbody2D _rigid;
- [SerializeField]
- private float _jumpForce = 5.0f;
- private bool _resetJump = false;
- [SerializeField]
- private float _speed = 5.0f;
- private PlayerAnimation _anim;
- void Start()
- {
- _rigid = GetComponent<Rigidbody2D>();
- _anim = GetComponent<PlayerAnimation>();
- }
- void Update()
- {
- Movement();
- }
- void Movement()
- {
- float move = Input.GetAxisRaw("Horizontal");
- if (Input.GetKeyDown(KeyCode.Space) && IsGrounded() == true)
- {
- Debug.Log("Jump!");
- _rigid.velocity = new Vector2(_rigid.velocity.x, _jumpForce);
- StartCoroutine(ResetJumpRoutine());
- }
- _rigid.velocity = new Vector2(move * _speed, _rigid.velocity.y);
- _anim.Move(move);
- }
- bool IsGrounded()
- {
- RaycastHit2D hitinfo = Physics2D.Raycast(transform.position, Vector2.down * 0.6f, 1 << 8);
- if (hitinfo.collider != null)
- {
- if (_resetJump == false)
- return true;
- }
- return false;
- }
- IEnumerator ResetJumpRoutine()
- {
- _resetJump = true;
- yield return new WaitForSeconds(0.1f);
- _resetJump = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement