Advertisement
Guest User

PlatformerController

a guest
Jul 21st, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlatformerController : MonoBehaviour
  5. {
  6.     PlatformerPhysics mPlayer;
  7.     bool mHasControl;
  8.  
  9.     void Start ()
  10.     {
  11.         mHasControl = true;
  12.         mPlayer = GetComponent<PlatformerPhysics>();
  13.         if (mPlayer == null)
  14.             Debug.LogError("This object also needs a PlatformerPhysics component attached for the controller to function properly");
  15.     }
  16.  
  17.     void Update ()
  18.     {
  19.         //here are the actions that are triggered by a press or a release
  20.         if (mPlayer && mHasControl)
  21.         {
  22.             if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift))
  23.                 mPlayer.StartSprint();
  24.  
  25.             if (Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift))
  26.                 mPlayer.StopSprint();
  27.  
  28.             if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
  29.                 mPlayer.Crouch();
  30.  
  31.             if (Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.DownArrow))
  32.                 mPlayer.UnCrouch();
  33.         }
  34.     }
  35.  
  36.     void FixedUpdate()
  37.     {
  38.         //here are actions where the buttons can be held for a longer period
  39.         if (mPlayer && mHasControl)
  40.         {
  41.             if (Input.GetButton("Jump"))
  42.                 mPlayer.Jump();
  43.  
  44.             mPlayer.Walk(Input.GetAxisRaw("Horizontal"));
  45.         }
  46.     }
  47.  
  48.     public void GiveControl() { mHasControl = true; }
  49.     public void RemoveControl() { mHasControl = false; }
  50.     public bool HasControl() { return mHasControl; }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement