Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Movement : MonoBehaviour {
- public float speed = 5f;
- public float jumpForce = 8f;
- public Transform groundCheck;
- float groundedRadius = .2f;
- public LayerMask groundMask;
- Rigidbody2D _rigidbody2D;
- bool grounded;
- void Start ()
- {
- _rigidbody2D = rigidbody2D;
- }
- void FixedUpdate()
- {
- grounded = Physics2D.OverlapCircle (groundCheck.position, groundedRadius, groundMask);
- }
- void Update ()
- {
- float x = Input.GetAxis ("Horizontal");
- _rigidbody2D.velocity = new Vector2 (speed * x, rigidbody2D.velocity.y);
- if(Input.GetButtonDown("Jump") && grounded)
- {
- _rigidbody2D.AddForce (new Vector2(0, jumpForce), ForceMode2D.Impulse);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement