Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- public class movement : MonoBehaviour
- {
- public float maxSpeed = 10f;
- public float maxJumpSpeed = 100f;
- public LayerMask groundLayer;
- public float distance = 0.4f;
- // Start is called before the first frame update
- void Start()
- {
- }
- bool IsGrounded()
- {
- Vector2 position = transform.position;
- Vector2 direction = Vector2.down;
- RaycastHit2D hit = Physics2D.Raycast(position, direction, distance, groundLayer);
- if (hit.collider != null)
- {
- return true;
- }
- return false;
- }
- // Update is called once per frame
- void Update()
- {
- float move = Input.GetAxis("Horizontal");
- GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
- if (IsGrounded() && Input.GetAxis("Vertical") > 0)
- {
- GetComponent<Rigidbody2D>().AddForce( new Vector2(0, maxJumpSpeed ));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement