Advertisement
Miziziziz

MovingAndJumping

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