Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [SerializeField]
- private float speed;
- [SerializeField]
- private float jumpHeight;
- [SerializeField]
- private float gravity;
- [SerializeField] private Vector3 direction;
- private new Rigidbody2D rigidbody2D;
- private void Awake()
- {
- rigidbody2D = GetComponent<Rigidbody2D>();
- }
- private void Update()
- {
- if (!OnGround())
- {
- direction.y -= gravity * Time.deltaTime;
- return;
- }
- if (Input.GetKeyDown(KeyCode.Space))
- direction.y = jumpHeight;
- direction = new Vector3(Input.GetAxis("Horizontal") * speed, direction.y, direction.z);
- }
- private void FixedUpdate()
- {
- rigidbody2D.velocity = direction;
- }
- private bool OnGround()
- {
- return Physics2D.Raycast(transform.position, Vector2.down, 1, LayerMask.GetMask("Floor"));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement