Advertisement
Miziziziz

Moving and Jumping

Nov 6th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.74 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Movement : MonoBehaviour {
  5.  
  6.     public float speed = 5f;
  7.     public float jumpForce = 8f;
  8.  
  9.     public Transform groundCheck;
  10.     float groundedRadius = .2f;
  11.     public LayerMask groundMask;
  12.  
  13.     Rigidbody2D _rigidbody2D;
  14.  
  15.     bool grounded;
  16.  
  17.     void Start ()
  18.     {
  19.         _rigidbody2D = rigidbody2D;
  20.     }
  21.    
  22.     void FixedUpdate()
  23.     {
  24.         grounded = Physics2D.OverlapCircle (groundCheck.position, groundedRadius, groundMask);
  25.     }
  26.     void Update ()
  27.     {
  28.         float x = Input.GetAxis ("Horizontal");
  29.         _rigidbody2D.velocity = new Vector2 (speed * x, rigidbody2D.velocity.y);
  30.  
  31.         if(Input.GetButtonDown("Jump") && grounded)
  32.         {
  33.             _rigidbody2D.AddForce (new Vector2(0, jumpForce), ForceMode2D.Impulse);
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement