Guest User

characterController.cs

a guest
Sep 9th, 2014
38,250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class characterController : MonoBehaviour {
  5.     public float maxSpeed = 10f;
  6.     public float jumpForce = 700f;
  7.     bool facingRight = true;
  8.     bool grounded = false;
  9.     public Transform groundCheck;
  10.     public float groundRadius = 0.2f;
  11.     public LayerMask whatIsGround;
  12.  
  13.     public float move;
  14.  
  15.     // Use this for initialization
  16.     void Start () {
  17.  
  18.     }
  19.    
  20.     // Update is called once per frame
  21.     void FixedUpdate () {
  22.  
  23.  
  24.         grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
  25.  
  26.         move = Input.GetAxis ("Horizontal");
  27.  
  28.     }
  29.  
  30.     void Update(){
  31.         if (grounded && (Input.GetKeyDown (KeyCode.W)||Input.GetKeyDown (KeyCode.UpArrow))) {
  32.  
  33.             rigidbody2D.AddForce (new Vector2(0f,jumpForce));
  34.         }
  35.         rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
  36.        
  37.         if (move > 0 && !facingRight)
  38.             Flip ();
  39.         else if (move < 0 && facingRight)
  40.             Flip ();
  41.  
  42.  
  43.  
  44.         if (Input.GetKey(KeyCode.Escape))
  45.         {
  46.             Application.Quit();
  47.         }
  48.  
  49.         if (Input.GetKey(KeyCode.R))
  50.         {
  51.             Application.LoadLevel(Application.loadedLevel);
  52.         }
  53.  
  54.  
  55.     }
  56.    
  57.     void Flip(){
  58.         facingRight = !facingRight;
  59.         Vector3 theScale = transform.localScale;
  60.         theScale.x *= -1;
  61.         transform.localScale = theScale;
  62.     }      
  63. }
Advertisement
Add Comment
Please, Sign In to add comment