Advertisement
Guest User

Untitled

a guest
May 6th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.66 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. namespace RootMotion.Demos {
  5.  
  6.     /// <summary>
  7.     /// User input for a third person character controller.
  8.     /// </summary>
  9.     public class UserControlThirdPerson : MonoBehaviour {
  10.  
  11.         // Input state
  12.         public struct State {
  13.             public Vector3 move;
  14.             public Vector3 lookPos;
  15.             public bool crouch;
  16.             public bool jump;
  17.             public int actionIndex;
  18.         }
  19.  
  20.         public bool walkByDefault;        // toggle for walking state
  21.         public bool canCrouch = true;
  22.         public bool canJump = true;
  23.  
  24.         public State state = new State();           // The current state of the user input
  25.  
  26.         protected Transform cam;                    // A reference to the main camera in the scenes transform
  27.  
  28.         void Start () {
  29.             // get the transform of the main camera
  30.             cam = Camera.main.transform;
  31.         }
  32.  
  33.         protected virtual void Update () {
  34.             // read inputs
  35.             state.crouch = canCrouch && Input.GetKey(KeyCode.C);
  36.             state.jump = canJump && Input.GetButton("Jump");
  37.  
  38.             float h = Input.GetAxisRaw("Horizontal");
  39.             float v = Input.GetAxisRaw("Vertical");
  40.            
  41.             // calculate move direction
  42.             state.move = Quaternion.LookRotation(new Vector3(cam.forward.x, 0f, cam.forward.z).normalized) * new Vector3(h, 0f, v).normalized;
  43.  
  44.             bool walkToggle = Input.GetKey(KeyCode.LeftShift);
  45.  
  46.             // We select appropriate speed based on whether we're walking by default, and whether the walk/run toggle button is pressed:
  47.             float walkMultiplier = (walkByDefault ? walkToggle ? 1 : 0.5f : walkToggle ? 0.5f : 1);
  48.  
  49.             state.move *= walkMultiplier;
  50.            
  51.             // calculate the head look target position
  52.             state.lookPos = transform.position + cam.forward * 100f;
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement